Database nested query, how to solve the asynchronous problem.

encountered a small problem when practicing:

clipboard.png
videouseruseriduseridavatar,videouser:

clipboard.png

then res.send () will execute before User.findOne () .

how can I execute res.send () after User.findOne () ?

the newcomer at the front end understands Promise and async/await to the extent that he can read it, but cannot write it.

Mengxin asks for advice!

Mar.02,2021

//async function
let videos = await Video.find(...);
for(let video of videos){
  await User.findOne(...)
}
res.send(...)

or execute multiple User.findOne with Promise.all.


there may still be problems after solving the sequence problem. Video should be read-only

Menu