-2

Here i am trying to get latest record key p_id value from mongodb collection but i am getting error like :SyntaxError: await is only valid in async function. So how to resolve this issue?

data.controller.js:

module.exports.getData = (req, res, next) => {
 var Product = mongoose.model(req.query.collectionname);
 const getid = await Product.findOne({ p_id: -1 }).limit(1)
 console.log(getid.p_id) 
}
Kavitha K
  • 75
  • 1
  • 11

1 Answers1

2

You need to make this function as an async function by putting async keyword in front of your function like bellow.

module.exports.getData = async(req, res, next) => {
 var Product = mongoose.model(req.query.collectionname);
 const getid = await Product.findOne({ p_id: -1 }).limit(1)
 console.log(getid.p_id) 
}
Kasunaz
  • 573
  • 5
  • 17