Link to home
Start Free TrialLog in
Avatar of Britt Thompson
Britt ThompsonFlag for United States of America

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) -

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);
      });
});

Open in new window

Avatar of leakim971
leakim971
Flag of Guadeloupe image

server side should handle this query check how many dot you have
you did not posted server side code handler
Avatar of Britt Thompson

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;

Open in new window

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 ?
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');

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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;

Open in new window

Sorry for the slow response here. I'll give this a try today if I have a minute. Thanks!
Worked perfectly. Thanks!

The other solution returned all results no matter what for some reason.
You are welcome.