猫鼬功能顺序

时间:2017-04-30 14:07:04

标签: node.js mongodb csv mongoose

我正在使用node.js将CSV表上的值上传到远程数据库。

CSV表包含有关企业及其提供的服务的信息。除非建立信息为空,否则每行对应一个新建立,如果建立信息为空,则它只读取服务信息并将其添加到最后列出的企业(这样您就可以为企业提供多种服务)。

将此信息读入节点很好,它以正确的顺序读取并适当地分隔它。我遇到的问题是使用mongoose上传值。

通过mongoose.save上传新的场所(因为它们是新条目),并通过mongoose.update上传服务更新。

新成立:

if (name != "" && name != lastName)
{
  console.log("New Establishment added")

  lastName  = name;

  var newEstab  = new establishment({
    name: name,
    location: { lat: lat, long: long },
    place_id: place_id,
    username: username,
    password: pass,
    details: { bio: bio,
              wait_time: wait,
              opening_monday: opening_monday,
              opening_tuesday: opening_tuesday,
              opening_wednesday: opening_wednesday,
              opening_thursday: opening_thursday,
              opening_friday: opening_friday,
              opening_saturday: opening_saturday,
              opening_sunday: opening_sunday,
              closing_monday: closing_monday,
              closing_tuesday: closing_tuesday,
              closing_wednesday: closing_wednesday,
              closing_thursday: closing_thursday,
              closing_friday: closing_friday,
              closing_saturday: closing_saturday,
              closing_sunday: closing_sunday
            },
    service: [{
      name: sname,
      price: sprice,
      time_taken: time_taken,
      service_category: scat,
    }]
  })
  ;



  newEstab.save( (err, finish) => {
    if (err) console.log("Error on Entry : " + err), reject(err)//; console.log("error" + err)
      else console.log("Establishment Uploade") //, resolve(finish)//; console.log("went through" + book)
    } )
 }

服务更新:

console.log("Service update on " + lastName)
//console.log("work with me: " + sname + " " + sprice + " " + time_taken + " " + scat);

    establishment.update(
      { name: lastName },

      {"$push" :
        {
          'service' : { 'name' : sname,
                        'price' : sprice,
                        'time_taken' : time_taken,
                        'service_category' : scat,
                      },
          }
      },
      // THE PROBLEM IS THE ORDER THEY"RE CARRIED OUT ^^^ PUSH HAPPENS BEFORE ESTABLISHMENT
      function (err, model)
      {
          new Promise((resolve, reject) => {
          if (err)
          {
            console.log("Error on Service update " + err)
            reject(err)
          } else {
            console.log("Successful Service update ")
            resolve(model)
          }
        })
      })

由于某种原因,mongoose总是在Insert之前运行Update,这意味着它尝试更新不存在且无法完成操作的企业。更新功能没有问题,就好像它之后单独运行它正确上传一样。这只是mongoose运行函数的顺序。

下面是终端输出显示的屏幕截图,即使在更新之前调用插入函数,它们也以不同的顺序执行。 Terminal Output

如果有人能帮助解释为什么mongoose这样做或提供改变它的解决方案 - 我会非常感激。

感谢您的时间。

0 个答案:

没有答案