Data null after saving entity with Moongose and GraphQL

人盡茶涼 提交于 2019-12-06 15:37:17

You're mixing Promises and callbacks. exec() will return a Promise, but only if doesn't have any arguments passed to it. Additionally, you need to return the Promise that's returned by exec().

return budget.save().then((res) => {
  return Budget.findById(res._id) // missing return here
    .populate('User')
    .populate('Vehicle')
    .exec() // don't need anything else
})

You can clean this up a little more:

return budget.save()
  .then(res => Budget.findById(res._id)
    .populate('User')
    .populate('Vehicle')
    .exec())

If you need to transform the results returned by findById before turning them over to the client:

return budget.save()
  .then(res => Budget.findById(res._id)
    .populate('User')
    .populate('Vehicle')
    .exec())
  .then(res => {
    res.foo = 'Foo'
    return res
  })
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!