Reputation: 365
I'm new to sequelize. I'm using mysql as my database. I've a sequelize query which finds the email-id from the database. By using the result of this query, I got the ID of that row. Now I need to return this value. Can someone please help me how to do that.
Here is my code.
var userId = getUserId(email_address);
function getUserId(email_address) {
models.users.findOne({
where: {
email: email_address
}
}).then(function(result) {
if (result) {
var applicantId = result.id;
return applicantId; // This is what I need to do
} else {
console.log("Could not find Email");
}
});
}
Here I need to return the variable applicantId to the calling function.
Upvotes: 1
Views: 13003
Reputation: 1336
The Sequelize call to the database is asynchronous so you need to alter your code a bit to work with promises. Something like this:
function getUserId(email_address) {
return models.users.findOne({
where: {
email: email_address
}
});
}
getUserId("[email protected]").then(function(result){
console.log(result.id);
});
check out this fiddle that mocks your scenario
Upvotes: 4