How to use a synchronous queries with mongoose on NodeJS server How to use a synchronous queries with mongoose on NodeJS server mongoose mongoose

How to use a synchronous queries with mongoose on NodeJS server


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.