简体   繁体   中英

Nodejs query from URL not working

I have a JS Array declared like this:

var phones = [
    {name: "Redmi Note 5" , manufacturer: "Redmi" , price: 9999},
    ...
];

I listen to this server with the following code:

app.listen(9999, function (err) {
    if (err) {
    throw err
    }
    console.log('Server started on port 9999');
})

This is the function I'm utilizing:

app.get('/get-items' , function(req , res) {
    var resu = phones;
    var man = (req.query['manufacturer'] === '');
    var mod = (req.query['name'] === '');
    console.log(man);
    console.log(mod);
    if (man)
        sortman(req , resu);
    if (mod)
        sortmod(req , resu);
    var jsonResult = JSON.stringify(resu);
    res.set('Content-Type', 'application/json');
    res.send(jsonResult);
})

If I give a URL like http://localhost:9999/get-items?manufacturer=Samsung , I get both the log as false, and even if I don't give any parameter, both the logs still show false.

I would like to know whether I am indeed supposed to use req.query['name'] or should I use something else?

EDIT: I have the required dependencies declared using require()

That's because :

req.query['manufacturer'] is equal to 'Samsung' and 
req.query['name'] is equal to undefined.

undefined and '' (empty string) are two different things in javascript. And they are not equal.

If you want to check for all of these, use isEmpty() from lodash

var _ = require('lodash')
app.get('/get-items' , function(req , res) {
    var resu = phones;
    if (_.isEmpty(req.query['manufacturer']))
        sortman(req , resu);
    if (_.isEmpty(req.query['name']))
        sortmod(req , resu);
    var jsonResult = JSON.stringify(resu);
    res.set('Content-Type', 'application/json');
    res.send(jsonResult);
})

isEmpty(val) will return true, if val is any of these: [empty Object, empty string, empty array, nil, undefined]

You probably want to assign the manufacturer and the name to the variables mod and man .

app.get('/get-items' , function(req , res) {
    var resu = phones;
    var man = !!req.query['manufacturer'];
    var mod = !!req.query['name'];
    console.log(man);
    console.log(mod);
    if (man)
        sortman(req , resu);
    if (mod)
        sortmod(req , resu);
    var jsonResult = JSON.stringify(resu);
    res.set('Content-Type', 'application/json');
    res.send(jsonResult);
})

Then when you call with ?manufacturer=Samsung , you will have in your log Samsung and the sorting function will be called.

EDIT: You can coerce anything to boolean with the double exclamation mark !!req.query['manufacturer'] will always return true / false, depending on if you have provided it.

You can read more about the operator here : What is the !! (not not) operator in JavaScript?

Try this, If you pass any value to manufacturer or name. It'll check for an empty string and undefined.

app.get('/get-items', function (req, res) {
    var resu = phones;
    if (req.query.manufacturer) // ?manufacturer=Samsung
        sortman(req, resu);
    if (req.query.name)    // ?name=xyz
        sortmod(req, resu);
    var jsonResult = JSON.stringify(resu);
    res.set('Content-Type', 'application/json');
    res.send(jsonResult);
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM