Reputation: 2611
I tried to make a synchronous queries using mongoose ODM using 'await' keyword basing on another post as the example bellow :
const query= userModel.find({});
const syncResutlt= await query.exec();
console.log(syncResutlt);
but I got this error message :
const result2 = await query.exec();
^^^^^
SyntaxError: Unexpected identifier
I tried also yield
generator keyword, but I get always the same error
SyntaxError: Unexpected identifier
for information I have nodeJs V8.
Upvotes: 1
Views: 991
Reputation: 624
You can only await Promises or a function marked as async, which essentially returns a Promise.
Correct Way
let getUser=async function(user_id){
let info= await User.findById(user_id);
console.log(info); // contains user object
}
Incorrect Way
let getUser= function(user_id){
let info= await User.findById(user_id); //will throw an exception on await keyword
console.log(info); // contains user object
}
Hope it helps.
Upvotes: 3