I have an if-else block in the function. The result depends on the value of q. If the value of q is a "well-defined query"(ex:localhost:1235/docs/q?beaven) then it should print 4. If the query is not well defined ie. q={}(ex: localhost:1235/docs), the result should have code
and message
as their properties(with their respective values) and passed as JSON.
But, it is not happening. I am messing up with the scope. The Output is: ""
function doSearch(app){
return errorWrap(async function(req, res) {
const q = req.query || {};
try {
// let results = await app.locals.finder.find(q);
let results = "";
// console.log(q);
if(q === {}){
results = {
code: "BAD_PARAM",
message: "required query parameter \"q\" is missing"
};
res.json(results);
// results = await app.locals.finder.find(q);
}else{
results == 4;
res.json(results);
}
}
catch (err) {
const mapped = mapError(err);
res.status(mapped.status).json(mapped);
}
});
}
if (q === {})
will never be true because ===
tests to see if the two are the exact same object (not if they have the same properties) and that will never be the case here because q
and {}
are always different objects.
You probably need to test if specific properties are present on q
or not. Or you can test if (Object.keys(q).length === 0)
to see if q
is an empty object. But, I think probably what is best for your code is to see if the specific properties you're looking for on q
are present or not.
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.