Britt Thompson
asked on
Node.js API router query string search for values containing the query string
I've created a custom API for internal use and have query strings used to find specific assets - http://localhost:3000/api/asset?ipaddress=10.1.1.100 ... returns 1 device
What I would like to do is return results for all devices with the ip address that contains the substring in the query string - http://localhost:3000/api/asset?ipaddress=10.1.1 ... returns all devices within this subnet. Right now, this returns 0 devices.
Here's the structure of my router(s) -
What I would like to do is return results for all devices with the ip address that contains the substring in the query string - http://localhost:3000/api/asset?ipaddress=10.1.1 ... returns all devices within this subnet. Right now, this returns 0 devices.
Here's the structure of my router(s) -
router.get('/', verifyToken, function (req, res) {
const query = {};
if(req.query.clientcode)
query.clientcode = req.query.clientcode;
if(req.query.assetkey)
query.assetkey = req.query.assetkey;
if(req.query.name)
query.name = req.query.name;
if(req.query.siteid)
query.siteid = req.query.siteid;
if(req.query.ipaddress)
query.ipaddress = req.query.ipaddress;
asset.find(query,function(err, results) {
return res.json(results);
});
});
ASKER
Just started using Node a couple months ago so you'll have to forgive my ignorance in knowing what to show you when you request the server side code handler. Here's my entire controller for this piece of the API. This is only controller using query strings - minus the update, remove, post.... I can ingest the data and filter it from the front end but as the data set grows I'd prefer to limit the size of my queries with query strings. The database is MongoDB.
import { vT as verifyToken } from '../auth/verifyToken';
import express from 'express';
import bodyParser from 'body-parser';
import { asset } from './asset';
const router = express.Router();
router.use(bodyParser.json({ limit:'50mb' }));
router.use(bodyParser.urlencoded({ extended:true ,limit:'50mb', parameterLimit:50000 }));
router.get('/', verifyToken, function (req, res) {
const query = {};
if(req.query.clientcode)
query.clientcode = req.query.clientcode;
if(req.query.assetkey)
query.assetkey = req.query.assetkey;
if(req.query.name)
query.name = req.query.name;
if(req.query.siteid)
query.siteid = req.query.siteid;
if(req.query.ipaddress)
query.ipaddress = req.query.ipaddress;
asset.find(query,function(err, results) {
return res.json(results);
});
});
export const assetController = router;
don't worry, it's my mistake : I did not checked the topics of your question and thought we was on client side...
the key of your issue is in the asset(.js(?) file, could you post it ?
the key of your issue is in the asset(.js(?) file, could you post it ?
ASKER
import mongoose from 'mongoose';
const assetSchema = new mongoose.Schema({
assignedto: String,
clientcode: String,
siteid: Number,
name: String,
status: String,
type: Number,
model: String,
manufacturer: String,
location: String,
assettag: String,
assetkey: String,
customerassettag: String,
serial: String,
firmware: String,
ipaddress: String,
externalipaddress: String,
macaddress: String,
loginurl: String,
operatingsystem: String,
servicepack: String,
cputype: String,
cpu: String,
ram: String,
domain: String,
role: String,
logicaldrives:[
{
id: String,
capacity: Number,
spacefree: Number
}
],
physicaldrives:[
{
id: String,
capacity: Number
}
],
productkey: String,
purchasedate: Date,
warrantyenddate: Date,
service: String,
notes: String,
software: String,
datecreated: Date,
datemodified: Date
});
mongoose.model('asset', assetSchema);
export const asset = mongoose.model('asset');
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
try the following :
import { vT as verifyToken } from '../auth/verifyToken';
import express from 'express';
import bodyParser from 'body-parser';
import { asset } from './asset';
const router = express.Router();
router.use(bodyParser.json({ limit:'50mb' }));
router.use(bodyParser.urlencoded({ extended:true ,limit:'50mb', parameterLimit:50000 }));
router.get('/', verifyToken, function (req, res) {
const query = {};
if(req.query.clientcode)
query.clientcode = req.query.clientcode;
if(req.query.assetkey)
query.assetkey = req.query.assetkey;
if(req.query.name)
query.name = req.query.name;
if(req.query.siteid)
query.siteid = req.query.siteid;
if(req.query.ipaddress && (req.query.ipaddress.split(".") == 3))
query.ipaddress = new RegExp("^" + req.query.ipaddress + "\..{1,3}");
if(req.query.ipaddress && (req.query.ipaddress.split(".") == 4))
query.ipaddress = req.query.ipaddress;
asset.find(query,function(err, results) {
return res.json(results);
});
});
export const assetController = router;
ASKER
Sorry for the slow response here. I'll give this a try today if I have a minute. Thanks!
ASKER
Worked perfectly. Thanks!
The other solution returned all results no matter what for some reason.
The other solution returned all results no matter what for some reason.
You are welcome.
you did not posted server side code handler