1

I'm having trouble in figuring this to work, I have one table from MongoDB (collection) for comments and another collection for Users.

When the page load it looks up the comment collection and selects the relevant comments, and then it searches the user table to find the name of the user who made the comment, the data will be combined and then the response is sent.

However, the output is sent before the data is fetched from the user table and added. How can I fix this, here is my code

  var output = []
  const Comments = require('comments.js')
  const Users = require('users.js')

  function delay( ) {
    return new Promise(resolve => setTimeout(resolve, 300))
  }

  async function delayedProcess(item) {
    await delay()
    Users.findById(item.user, async function(err, result) {
      Object.assign(item, {name: result.name})
      output.push(item)
    })
  }

  async function processArray(array) {
    const promises = array.map(delayedProcess)
    await Promise.all(promises)
    return res.json({data: output})
  }


  Comments.find({page_id: id}).sort({post_date:-1}).limit(6).then(function(data) {
    processArray(data)
  })

1 Answer 1

1

You are not returning promise from the delayedProcess function.

There you go :-

  const Comments = require('comments.js')
  const Users = require('users.js')

  const output = []
  function delayedProcess(item) {
    return new Promise((resolve, reject) => {
      Users.findById(item.user, function(err, result) {
        if (err) return reject (err);
        output.push({ ...item, name: result.name })
        return resolve()
      })
    })
  }

  async function processArray(array) {
    const promises = array.map(async(item) => {
      await delayedProcess(item)
    })
    await Promise.all(promises)
    return res.json({ data: output })
  }

  const data = await Comments.find({ page_id: id }).sort({ post_date: -1 }).limit(6)
  processArray(data)

However you will always get the concatenated array. So instead taking it globally, take it as local variable

  function delayedProcess(item) {
    return new Promise((resolve, reject) => {
      Users.findById(item.user, function(err, result) {
        if (err) return reject (err);
        return resolve({ ...item, name: result.name })
      })
    })
  }

  async function processArray(array) {
    const output = []
    const promises = array.map(async(item) => {
      const it = await delayedProcess(item)
      output.push(it)
    })
    await Promise.all(promises)
    return res.json({ data: output })
  }

  const data = await Comments.find({ page_id: id }).sort({ post_date: -1 }).limit(6)
  processArray(data)

More simplified :- Since mongodb queries itself returns promise you do not need to use new Promise syntax.

  async function processArray() {
    const array = await Comments.find({ page_id: id }).sort({ post_date: -1 }).limit(6)
    const output = []
    const promises = array.map(async(item) => {
      const it = await Users.findById(item.user).lean()
      item.name = it.name
      output.push(item)
    })
    await Promise.all(promises)
    return res.json({ data: output })
  }
Sign up to request clarification or add additional context in comments.

3 Comments

what's lean function, i can't find a reference
Mongoose returns mongoose object, lean() is used to convert it to plain object.
Thank you for the response you saved my life, I find this promises bit difficult to understand and I'm sure I'm not alone

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.