I’m not returning the result. That variable was left there when I was trying to return the result but got errors. Since the done() is called in the exec() so how can I return the variable?
What’s the reason to return the result. Isn’t the returned result a Query Object taht needs to be chained to the functions that are called after the find method and then the call to the done() to return the result? Also the challenge prompt doesn’t mentiond anything about returning the result. The first challenges clearly say that we’ll follow the node convention of calling done(err) in the case of errors and calling done(null, data) when there’s no error. I was able to pass all the previous challenges with that but now it’s failing on this challenge .
but this is not my code, it was there already and other methods such as removeManyPeople have the same variable. Changing ‘const’ to ‘let’ didn’t help the issue either.
Could you please answer my previous question so I can understand your logic of why to return from the function? I still don’t understand how that can help and why it is even possible to return from the find() call when there’s an exec() call in which we’re calling the done() function.
Okay so I updated the method to take an extra variable, here’s my new code
good question, done is returning its value to within “find()”, if you want this value to be passed to “caller of queryChain method” then you need to return something from it, otherwise it will get “undefined”
may be others can chime in, and correct me if im saying anything wrong here
Thanks so much for helping. I did try it with the return in front of it and it still errors out with the same error but looking up online it seems to be a bad practice to return from the function in this way when the done() is being called.
Could you please explain a bit more details about the caller of the function and why it receives undefined but none of the challenges before needed a return statement? What makes this single challenge so different that we need to now return the result to the caller?
I put a console.log() inside the tester method in the server.js file, right before the call to res.json(data) I’m console logging data and it’s containing the query result that seems to be correct. so can someone please help me figure out what is causing the challenge to not pass and throw an error? I’ll post the console result and the error as well. Thanks for helping!
server.js
const chain = require("./myApp.js").queryChain;
router.post("/query-tools", function (req, res, next) {
let t = setTimeout(() => {
next({ message: "timeout" });
}, TIMEOUT);
Person.deleteOne({}, function (err) {
if (err) {
return next(err);
}
Person.create(req.body, function (err, pers) {
if (err) {
return next(err);
}
try {
chain(function (err, data) {
clearTimeout(t);
if (err) {
return next(err);
}
if (!data) {
console.log("Missing `done()` argument");
return next({ message: "Missing callback argument" });
}
console.log(`data: ${data}`)
res.json(data);
});
} catch (e) {
console.log(e);
return next(e);
}
});
});
});
console result
Your app is listening on port 3000
OPTIONS
POST
data: {
favoriteFoods: [ 'steak', 'burrito' ],
_id: 6447119a2a4037010a9b6ab1,
name: 'Ashley',
__v: 0
},{
favoriteFoods: [ 'steak', 'burrito' ],
_id: 6447116523489300eadf2303,
name: 'Ashley',
__v: 0
}
as suggested by @bappyasif , I tried returning the result of the chain calls in the queryChain function as well as tried to pass a second argument to the queryChain function and change foodToSearch to be set to this passed argument. none of these worked though. I also tried to use a function inside the exec() instead of the arrow function but this didn’t work either.
Thanks so much for helping me figure this out. I started from scratch and that reverted those methods in the server.js for me and now my solution in myApp.js works. The reason I changed those method was because I found someone in one of the page suggesting that change.