NodeJs异步发布值猫鼬保存并查找

时间:2018-11-10 17:47:28

标签: javascript node.js async-await

我已经动脑了几个小时,希望您能帮我!

const Construct = require('../models/constructModel')
const TemplateConstruct = require('../models/constructTemplateModel')

exports.create = function () {
  let universeConstructs = TemplateConstruct.get(async function (err, constructs) {
    if (err) {
      return false
    } else {
      let table = []
      await constructs.forEach((construct) => {
        let newconstruct = new Construct()
        newconstruct.number = construct.number
        newconstruct.name = construct.name
        newconstruct.basePrice = construct.basePrice
        newconstruct.baseMicrowave = construct.baseMicrowave
        newconstruct.atomGain = construct.atomGain
        newconstruct.save(function (err) {
          if (err) {
            return false
          } else {
            table.push(newconstruct)
          }
        })
      })
      console.log('table : ' + table)
      return table
    }
  })
  console.log('constructs : ' + universeConstructs)
  return universeConstructs
}

console.log表或构造均未返回我期望的结果。

constructs : undefined
table : 

0 个答案:

没有答案