The console statement never prints. I am not sure why. I am writing a function to list all characters (one user has many characters). The function isn't fully written yet. It simply returns the user with the specified email.
For some reason though it never returns. It outputs "This statement prints" But never the "why doesn't this ever print" statement!
UserSchema.methods.usersCharacters = function(email,cb){
User.findOne( {'local.email' : email }).exec(function(err, user){
if (err) console.log("oops");
console.log("This statement prints");
return user;
});
};
UserSchema.methods.usersCharacters('a#gmail.com', function(err, okay){
console.log("Why doesn't this ever print?");
});
I needed to call the callback! DANGIT!
You need to call the callback ,see this :
var a = function(input ,callback){
if ( input == "true" ){
callback(null ,"it's true");
} else {
callback(true ,"it's false")
}
};
a(true ,function(err ,res){
console.log(err);
console.log(res);
});
a(false ,function(err ,res){
console.log(err);
console.log(res);
});
I hope this help you to understand how to write callbacks.
Related
I have an quite simple application the idea is that someone has unique code which value are stored in one mongo collection in other we are keeping some data which we need to return if the key was found in first collection.
As probably you have noticed I'm using NodeJS with MongoDB and Mongoose, Express.
I have a problem with method bellow:
exports.getCompanyByKey = function(req, res) {
console.log(req.params.keyvalue);
var query = Company.where({keyValue : req.params.keyvalue});
query.findOne(function(err, company){
if(err){
res.send(err);
}else{
SampleData.findOne({}, function(err, sample_data){
if(err)
res.send(err);
res.json(sample_data);
});
}
});
};
The problem is that it will always return the data beause it's not throwing an error but empty array - so is there any other good and proper way as it should be don to throw 404 error without statement such as if(length<0) res.status(404).send('Error message).
I simply want to minimalize amount of if statements.
Maybe there is some other way to write implementation od error handling for mongoose which in general instead returning empty array will give us error code with message?
It's not exactly clear what you're asking, but if you want to make an error condition out of something that is not normally an error, then an if statement (or some other test like that) is required to test for that specific condition.
You could make your own function for querying that turns an empty response into an error and you could "hide" the if condition in that function if you want, but it's still an if condition that tests for your specific condition.
So, to return a 404 if the array is empty, you would just add an if statement (as you already appear to know):
exports.getCompanyByKey = function(req, res) {
console.log(req.params.keyvalue);
var query = Company.where({keyValue : req.params.keyvalue});
query.findOne(function(err, company){
if(err){
res.status(500).send(err);
} else {
SampleData.findOne({}, function(err, sample_data){
if(err) {
res.status(500).send(err);
} else {
if (sample_data.length) {
res.json(sample_data);
} else {
res.status(404).send("no data");
}
}
});
}
});
};
FYI, you also need to make sure you are properly setting a status code when there's an error and that you are never sending multiple responses to the same request (even when there's an error). I've also fixed several cases of those issues in your code.
This could likely be written cleaner and responses consolidated by using the promise interface to your database and send an error in one .catch().
For example, you could simplify your code by creating a utility function for .findOne() that detects and sends an error response automatically:
function findOne(res, db, q, cb) {
db.findOne(q, function(err, data) {
if (err) {
res.status(500).send(err);
cb(err);
} else if (!q.length) {
res.status(404).send("no data");
cb(new Error("no data"));
} else {
cb(null, data);
}
});
}
Then, your function could be simplified to this:
exports.getCompanyByKey = function(req, res) {
var query = Company.where({keyValue : req.params.keyvalue});
query.findOne(function(err, company){
if(err){
res.status(500).send(err);
} else {
findOne(res, SampleData, {}, function(err, sample_data) {
// any error response has already been sent
if (!err) {
res.json(sample_data);
}
});
}
});
};
Again, this would be better to use your Db's promise interface.
I've run into an issue and I'm unsure what steps to take to fix it. Right now all the data is being correctly retrieved however the responseCallback is never firing and thus i'm not reaching the res.json call with the response array. Any guidance here would be greatly appreciated! Thanks.
For clarification the problem is with the aysnc.each callback.
var updatedBusinesses = [];
googleplaces.radarSearch({location:"lat,long",radius:"10000",keyword:"keywordhere"},function(err,response){
if(err){return next(err);}
async.each(response.results,function(currResponse,responseCallback){
Business.findOne({"placesId":currResponse.place_id,"claimed":true}).populate({path:'services',select:''}).exec(function(err,business){
if(err){return next(err);}
if(business !== null){
Service.populate(business.services,{path:'employees',select:'_id appointments firstName lastName username avatarVersion'},function(err,newBusiness){
if(err){return next(err);}
googleplaces.placeDetailsRequest({placeid:business.placesId},function(error,placesResult){
if(error){return responseCallback(error);}
console.log("RESULT OF THE GOOGLE PLACES DETAIL SEARCH")
placesResult.result.info = business;
updatedBusinesses.push(placesResult.result);
// Here the data is populated and correct.
// console.log(updatedBusinesses)
responseCallback();
});
})
}
})
},function(err){
if(err){return next(err);}
console.log("called")
res.json(updatedBusinesses);
})
})
This is where I'm hoping to return the updated business information to the client however this never fires.
},function(err){
if(err){return next(err);}
console.log("called")
res.json(updatedBusinesses);
})
})
async.each() expects a callback (responseCallback) to be called for every iteration. If it's not called, it's sitting there waiting for it. That's why your update business section never gets called.
Inside your async.each(), there are a number of places calling next() which is not async.each()'s iteration's callback (responseCallback). Here is the revised code that calls the callbacks properly:
var updatedBusinesses = [];
googleplaces.radarSearch({location:"lat,long",radius:"10000",keyword:"keywordhere"},function(err,response){
if(err){return next(err);}
async.each(response.results,function(currResponse,responseCallback){
Business.findOne({"placesId":currResponse.place_id,"claimed":true}).populate({path:'services',select:''}).exec(function(err,business){
if(err){
return responseCallback(err);// <== calling responseCallback instead of next()
}
// in case of business === null/undefined, I'm not seeing any
// callback getting called, it needs to be called inside
// async.each() no matter which condition it is
if (!business) {
// call responseCallback to continue on with async.each()
return responseCallback();
}
Service.populate(business.services,{path:'employees',select:'_id appointments firstName lastName username avatarVersion'},function(err,newBusiness){
if(err){
return responseCallback(err);// <== calling responseCallback instead of next()
}
googleplaces.placeDetailsRequest({placeid:business.placesId},function(error,placesResult){
if(error){return responseCallback(error);}
console.log("RESULT OF THE GOOGLE PLACES DETAIL SEARCH")
placesResult.result.info = business;
updatedBusinesses.push(placesResult.result);
// Here the data is populated and correct.
// console.log(updatedBusinesses)
responseCallback();
});
})
})
},function(err){
if(err){return next(err);}
console.log("called");
res.json(updatedBusinesses);
});
});
So now responseCallback() is called for every condition inside async.each(). It should get down to "updated business information" part of the code now.
Changing your code responseCallback(); with responseCallback(null, [your-result]); I think should do the job
var updatedBusinesses = []; googleplaces.radarSearch({location:"lat,long",radius:"10000",keyword:"keywordhere"},function(err,response){
if(err){return next(err);}
async.each(response.results,function(currResponse,responseCallback){ Business.findOne({"placesId":currResponse.place_id,"claimed":true}).populate({path:'services',select:''}).exec(function(err,business){
if(err){return next(err);}
if(business !== null){
Service.populate(business.services,{path:'employees',select:'_id appointments firstName lastName username avatarVersion'},function(err,newBusiness){
if(err){return next(err);}
googleplaces.placeDetailsRequest({placeid:business.placesId},function(error,placesResult){
if(error){return responseCallback(error);}
console.log("RESULT OF THE GOOGLE PLACES DETAIL SEARCH")
placesResult.result.info = business;
updatedBusinesses.push(placesResult.result);
// Here the data is populated and correct.
// console.log(updatedBusinesses)
responseCallback(null, updatedBusinesses);
});
})
} }) },function(err, updatedBusinesses){
if(err){return next(err);}
console.log("called")
res.json(updatedBusinesses);
})
})
thanks for your help...struggling big time with how to handle this properly. I'm in async now, having given up on my ability to write the callbacks properly. I have snippet where I'm passing a set of random numbers (eachrecord) and passing them through to a mongoose call. Trying to create a data set from the multiple queries I pass.
My issue is that no matter what I've done for 4 hours, the "newarray" variable is always empty.
Thank you for your help -
async.forEach(arLimit, function(eachrecord, callback){
newarray = new Array;
var query = UGC_DB_Model.find({}).skip(eachrecord).limit(-1);
query.execFind(function (err, data) {
if (err)
console.log(err);
else {
newarray.push(data);
}
});
callback(null, newarray);
}, function(err, result) {
if (err) return next(err);
console.log("(it's empty): " + result);
});
There are several issues with your code:
async.forEach isn't meant to 'generate' results, that's what async.map is for;
you need to call the callback only when execFind is done, and not immediately after calling it;
your newarray is probably not necessary;
So try this instead:
async.map(arLimit, function(eachrecord, callback){
var query = UGC_DB_Model.find({}).skip(eachrecord).limit(-1);
query.execFind(function (err, data) {
if (err)
callback(err); // pass error along
else {
callback(null, [ data ]);
// although I think you mean this (because 'data' is probably an array already)
// callback(null, data);
}
});
}, function(err, result) {
if (err) return next(err);
console.log("(it's empty): " + result);
});
I am implementing a restify middleware for session authentication. The function contains nested asynchronous db calls: db.sessions.remove() within the callback of db.sessions.findOne().
The 'return' statements are confusing me because I'm not sure if I am returning next() or next(err) from the callback back to verifyUserSession(), or is it just returning from verifyUserSessions? Am I doing this right?
function verifyUserSession(req, res, next) {
if (req.headers.sessionKey)
{
db.sessions.findOne(req.headers.sessionKey, function(err, session) {
if (err)
{
return next(err);
}
if (!session)
{
return next(new Error({'message': 'Session does not exist'}));
}
if ((new Date().getTime() - session.timestamp.getTime())/86400000 > 60)
{
db.sessions.remove({sessionKey: req.headers.sessionKey}, function(err){
if (err)
{
return next(err);
}
return next(new Error({'message': 'Session expired'}));
});
}
else
{
// session ok
}
});
}
}
You are using the callbacks just right.
return is used only to return from the current callback. When you call return next(...) you invoke the callback function and return the value it returns. Returns are often needed only to make sure you don't invoke a callback twice.
Note that you need to make sure every possible branch in your function will invoke the callback, otherwise your program will never proceed from verifyUserSession. In the code example you gave this happens two cases: 1) if session is ok, 2) if req.headers.sessionKey is not set. You should add callbacks for these branches as well.
Return statements in asynchronous coding are used only because they interrupt further execution of the block code. Basically this:
db.sessions.remove({sessionKey: req.headers.sessionKey}, function(err){
if (err)
{
return next(err);
}
return next(new Error({'message': 'Session expired'}));
});
is equivalent to:
db.sessions.remove({sessionKey: req.headers.sessionKey}, function(err){
if (err)
{
next(err);
return;
}
next(new Error({'message': 'Session expired'}));
return;
});
Note that last return is unnecessary.
I am little bit confused with my code it's not worked synchronusly as it's should be. I use everyauth to do authentication.
registerUser(function(newUserAttrs) {
var login = newUserAttrs[this.loginKey()];
user.CreateNewUser(newUserAttrs.login, newUserAttrs.password, newUserAttrs.email, function(res, err) {
if(!err) {
return usersByLogin[login] = newUserAttrs;
}
else {
throw err;
}
});
})
in another file I have write this code
exports.CreateNewUser = function(login, pass, mail, callback) {
var sql = "insert into `user`(login,mail,pass) values(?,?,?)";
client.query(sql, [login, mail, pass], function(err, results, fields) {
if(!err) {
callback(results);
console.log('test')
}
else {
callback(results, err);
}
});
};
This code are working fine. I have tested him. the only problem is they are working synchronosly (as normal). Can someone explain me what thing I have done in wrong way that make it async. I want to get it done in sync way.
The current code give me error (it's make a entry in database and produce error on browser)
Error: Step registerUser of `password` is promising: userOrErrors ; however, the step returns nothing. Fix the step by returning the expected values OR by returning a Promise that promises said values.
at Object.exec (E:\proj\Node\node_modules\everyauth\lib\step.js:68:11)
at E:\proj\Node\node_modules\everyauth\lib\stepSequence.js:26:38
at [object Object].callback (E:\proj\Node\node_modules\everyauth\lib\promise.js:13:12)
at RouteTriggeredSequence._bind (E:\proj\Node\node_modules\everyauth\lib\stepSequence.js:25:20)
at RouteTriggeredSequence.start (E:\proj\Node\node_modules\everyauth\lib\stepSequence.js:52:33)
at RouteTriggeredSequence.routeHandler (E:\proj\Node\node_modules\everyauth\lib\routeTriggeredSequence.js:13:13)
at Object.<anonymous> (native)
at nextMiddleware (E:\proj\Node\node_modules\connect\lib\middleware\router.js:175:25)
at param (E:\proj\Node\node_modules\connect\lib\middleware\router.js:183:16)
at pass (E:\proj\Node\node_modules\connect\lib\middleware\router.js:191:10)
Thanks
The two pieces of code you present are asynchronous and not synchronous!
With everyauth, to be able to handle asynchronous user creation you should use a Promise. So your code will be something like :
registerUser(function(newUserAttrs) {
var promise = this.Promise();
var login = newUserAttrs[this.loginKey()];
user.CreateNewUser(newUserAttrs.login, newUserAttrs.password, newUserAttrs.email, function(res, err) {
if(!err) {
return promise.fulfill(newUserAttrs);
}
else {
promise.fulfill(user);
}
});
})
Without promise you couldn't be sure that your new user has been added in your database. But if it doesn't matter you could have something like that:
registerUser(function(newUserAttrs) {
var login = newUserAttrs[this.loginKey()];
user.CreateNewUser(newUserAttrs.login, newUserAttrs.password, newUserAttrs.email, function(res, err) {
if (err) console.log(err);
});
return newUserAttrs;
})
Because you are doing a database query, this code has to be asynchronous. The anonymous function you pass to client.query will not be called until the database query is complete, so your callback gets called asynchronously.
You will need to treat this all as asynchronous, so for instance you'll have to trigger some other callback instead of returning the user object/throwing.