javascript - Structure of multiple nested Mongoose promises -
how structure function has multiple mongoose.findone()
nested in each other?
i need like
const userid = '...'; const postid = '...'; const imageid = '...'; user.findbyid(userid).then(user => { if (!user) { return res.status(400).json({ status: 'error', err: 'user not found', }); } post.findbyid(postid).then(post => { if (!post) { return res.status(400).json({ status: 'error', err: 'post not found', }); } image.findbyid(imageid).then(image => { if (!image) { return res.status(400).json({ status: 'error', err: 'image not found', }); // variables 'user', 'post', , 'image' }).catch(err => { .. }); }).catch(err => { .. }); }).catch(err => { .. });
since collection.findbyid()
returns promise, guess should use chaining instead of structure.
so might like
user .findbyid(userid) .then(user => post.findbyid(postid)) .then(post => image.findbyid(imageid)) .then(image => { // variables 'user', 'post', , 'image' }); .catch(err => { .. });
but don't know how access variables user
, post
, , image
, , how throw errors, can access them in catch
statement.
edit
i have tried this
async function getpostasync() { const userid = '597989c668189f31483ffdbf'; const postid = '597989c62624ea74750c74f8'; if (!userid) { throw new error('user id missing'); } if (!postid) { throw new error('post id missing'); } const user = await user.findbyid(userid); const post = await post.findbyid(postid); return post; } app.get('/', (req, res) => { getpostasync().then(post => { res.json({ status: 'success', }); }).catch(err => { res.status(400).json({ status: 'error', err }); }) });
but receive
{ "status": "error", "err": {} }
am doing wrong?
but same result with
async function getpostasync() { throw new error('msg'); return post.find(); }
so might calling async function wrong.
you can't access variables inside later promise's then
, can round assigning local resolved values global variables
let globaluser, globalpost; // create variables later user .findbyid(userid) .then(user => { globaluser = user; // assign global return post.findbyid(postid) }) .then(post => { globalpost = post; // assign global return image.findbyid(imageid) }) .then(image => { // variables 'globaluser', 'globalpost', , 'image' }) .catch(err => {... });
edit: or when using async/await
:
async function() { const user = await user.findbyid(userid); const post = await post.findbyid(postid); const image = await image.findbyid(imageid); // user, post , image }
seeing promises don't rely on each other use promise.all()
in async function:
async function() { const result = await promise.all([ user.findbyid(userid), post.findbyid(postid), image.findbyid(imageid) ]); const [user, post, image] = result; // user, post , image }
edit 2: error handling
async function getimage() { let user; try { user = await user.findbyid(userid); } catch (error) { // deal rejection of `user.findbyid` // error } // if these fail entire function throw const post = await post.findbyid(postid); const image = await image.findbyid(imageid); return image; } getimage() .then(image => {... }) .catch(error => {... }); // deal rejection of `getimage` whole
the above code showcases ways can handle errors in async function. first how deal error in user.findbyid
function, wrapping in try catch
block.
the second method letting entire async function throw error. i.e. if post.findbyid
or image.findbyid
promises reject, entire getimage()
promise reject, can deal in .catch()
handler.
Comments
Post a Comment