【问题标题】:Simple mongo object upsert using strongloop使用 strongloop 的简单 mongo 对象 upsert
【发布时间】:2016-07-29 14:35:56
【问题描述】:

我正在尝试对要更新的字段是数字类型的特定记录执行简单的 upsert 操作。 我使用它的记录 ID 获取记录,只是 +1 数字字段并再次保存它,但由于某种原因,它不会从日志中看到增量/更新

代码是:

customer.findById(customerObject.id, function(err, objectResult){

    console.log(objectResult.imageIndex);

    if(!objectResult.imageIndex){
        objectResult.imageIndex = 1;
    }
    else{
        var index = objectResult.imageIndex;
        objectResult.imageIndex = index++;
        console.log(objectResult.imageIndex);
    }

    customer.upsert(objectResult, function(err, response){});
})

console.log 都会显示相同的值。为什么它不会增加和更新,代码有什么问题吗?

【问题讨论】:

  • 登录 `typeof(objectResult.imageIndex)' 会得到什么?
  • 它的印刷品'数字'

标签: javascript node.js mongodb loopbackjs strongloop


【解决方案1】:

问题在于声明:

objectResult.imageIndex = index++;

这是因为 index++ 先做赋值,然后再增加 index 的值。

应该是:

objectResult.imageIndex = ++index;

objectResult.imageIndex = index + 1;

【讨论】:

  • 不错的收获!在发布问题后我也抓住了它:)
猜你喜欢
  • 1970-01-01
  • 2017-10-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-24
相关资源
最近更新 更多