0

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

6
  • It'll return false because you are passing samsung and nothing(undefined). what is expected output? Commented Mar 1, 2018 at 17:51
  • Ok, but at least one of them must be true, right? Commented Mar 1, 2018 at 17:52
  • use == instead of ===. what is expected output? Commented Mar 1, 2018 at 17:54
  • Same with == . When I give http://localhost:9999/get-items?manufacturer=Samsung it must be false true And when I give http://localhost:9999/get-items, it should be true true Commented Mar 1, 2018 at 17:56
  • how it is possible, you are telling 'samsung' == '' ? Commented Mar 1, 2018 at 17:57

3 Answers 3

1

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);
})
Sign up to request clarification or add additional context in comments.

4 Comments

And if I had a query like min-price, how would I write it here?
req.query[`min-price`]
@Sparker0i The problem was not that you were using ['data']. The problem was that you were also typing === ' '
Thanks, but even if I typed == it still wouldn't work
1

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]

Comments

1

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?

1 Comment

No, I want to check whether they have any values in them or not. If there are no values, I want to assign false. I'm sorry I don't want to store the values as such

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.