首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >值在允诺nodejs之后未定义

值在允诺nodejs之后未定义
EN

Stack Overflow用户
提问于 2017-07-24 19:22:08
回答 1查看 50关注 0票数 0

我的代码有问题..。我对我的数据库进行查询,以检查数据库上是否有一个macs数组的mac地址。如果有任何结果,则返回DB中的macs计数,如果是>0,则不会添加任何内容,因为mac已经列出,但是如果我的result.count =0,我将添加一个新记录。

我的新唱片里只有mac地址。为此,我试着:

代码语言:javascript
复制
var countRepetidos = 0
var countPromises = []

if (obj.data.list != {} && obj.data.list.length > 0) {
  var aux = obj.data["list"]
  countRepetidos = 0

  for (var i = 0; i < aux.length; i++) {
    countPromises.push(Database.Probing.getMacAdress(aux[i]).then(function(data) {
      console.log("probing countPromises aux[i] ", aux[i])

      if (data.count > 0) {
        countRepetidos += 1
      } else {
        Database.Probing.addMac(aux[i])
      }

      return Promise.resolve()
    }))
  }

  Promise.all(countPromises).then(() => {
    dataRepeated = [obj.data.stats.since, countRepetidos]
    listaRepeated.push(dataRepeated)

    console.log("probing listaRepeated --> ", listaRepeated)

    if (listaRepeated != [] && (listaRepeated[0][0] != undefined && listaRepeated[0][1] != undefined)) {
      Database.Probing.getLastTimestamp("probing_repeated", device.id).then(function(data) {
        var lastTimestamp = data.date_part

        console.log('probing lastTimestamp ', lastTimestamp * 1000)

        if (lastTimestamp != listaRepeated[0][0] / 1000) {
          Controllers.Agregate.agregateData("probing_repeated", 5 * 60, listaRepeated, dbHistConnectionString, device.id, device.network_id, device.organization_id, ["time", "clients"])
        }
      })
    }
  })
}

问题是,在Database.Probing.getMacAddress之后,我的auxi变得没有定义,我需要将这个值插入到我的DB中。

有人能帮忙吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-07-24 19:34:55

您需要保留i的值。你可以这样做:

代码语言:javascript
复制
for (var i = 0; i < aux.length; i++) {
  (function(i) {
    countPromises.push(
      Database.Probing.getMacAdress(aux[i]).then(function(data) {
        console.log("probing countPromises aux[i] ", aux[i])
        if (data.count > 0) {
          countRepetidos += 1
        } else {
          Database.Probing.addMac(aux[i])
        }

        return Promise.resolve()
      }))
  })(i)
}

编辑1:按照@lain的建议,使用let over var

代码语言:javascript
复制
 for (let i = 0; i < aux.length; i++) {}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45288483

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档