I wrote an async function in nodejs that return value of a query in my database. I tested this query and it worked. but my problem is in the define of readline. when i run this code I get an error that :
const a = await Movie.find({}).sort('-year').where('year').gt(X).lt(Y).sort('rank')
^^^^^
SyntaxError: await is only valid in async function
how should I define readline function as async function? this is my function :
async function returnMoviesBetweenXandY(){
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.question('enter the first number : ', (X) => {
rl.question('enter the second number : ', (Y) => {
const a = await Movie.find({}).sort('-year').where('year').gt(X).lt(Y).sort('rank')
const temp =await Promise.map(a, getTitle)
return temp
// rl.close();
});
});
}
returnMoviesBetweenXandY().then(function(result){console.log(result)})