简体   繁体   English

map 中的数组为空,返回 promise - 节点 JS

[英]Array empty in map returning promise - NODE JS

i am newbie on this.我是这方面的新手。 My problem is when I tried to return array full of values, it returned empty.我的问题是当我尝试返回充满值的数组时,它返回空。 I faced some weeks before and I solved it, declaring the map as a const and returning its value in a function... but I can't remeber how I did it.几周前我遇到了它,我解决了它,将 map 声明为const并在 function 中返回它的值......但我不记得我是怎么做到的。

This is my code:这是我的代码:

module.exports.deleteLockedEmail = (req, res, next) => {
  const maxDateAd = new Date().getTime() - 2592000000;
  const adIdsToDelete = [];
  Admin.find({})
    .then(emailLocked => {
      const mapToLockedEm =  emailLocked.map(element => {
        return User.findOne({email:element.email})
                .then(userLocked => {
                  return adIdsToDelete.push(userLocked)
                })
                .catch(error => console.log(error))
      })
      return mapToLockedEm
    })
    .catch(error => console.log(error))

  cron.schedule("* * * * *", function() {
  console.log("running a task every minute => delete locked ads");
  });
}

How can I fill this array?我怎样才能填充这个数组?

adIdsToDelete = [];

Remember that all these calls to database are asynchronous.请记住,所有这些对数据库的调用都是异步的。 To get the values you have to wait till the promise is resolved and return values.要获取值,您必须等到 promise 被解析并返回值。 In this case we can wait for all Promises with Promise.all to be executed and after that in next then context在这种情况下,我们可以等待所有带有 Promise.all 的 Promise 被执行, then在下一个上下文中执行

module.exports.deleteLockedEmail = (req, res, next) => {
  const maxDateAd = new Date().getTime() - 2592000000;
  const adIdsToDelete = [];
  Admin.find({})
    .then(emailLocked => {
      const mapToLockedEm =  emailLocked.map(element => {
        return User.findOne({email:element.email})
                .then(userLocked => {
                  return adIdsToDelete.push(userLocked)
                })
                .catch(error => console.log(error))
      })
      return Promise.all(mapToLockedEm)
    }).then(() => {
      // In this context the adIdsToDelete will be filled
    });
    .catch(error => console.log(error))

  cron.schedule("* * * * *", function() {
  console.log("running a task every minute => delete locked ads");
  });
}

Also in general it can be a little tricky to work with variables like adIdsToDelete as they are in different scope than your promise chain.此外,通常使用adIdsToDelete等变量可能有点棘手,因为它们与 promise 链位于不同的 scope 中。 As it happened in your example - it can be confusing when this variable actually fills with values.正如您的示例中发生的那样 - 当此变量实际填充值时可能会令人困惑。

You can rewrite it as following您可以将其重写如下

module.exports.deleteLockedEmail = (req, res, next) => {
  const maxDateAd = new Date().getTime() - 2592000000;
  Admin.find({})
    .then(emailLocked => {
      const mapToLockedEm =  emailLocked.map(element => {
        return User.findOne({email:element.email})
                .catch(error => console.log(error))
      })
      return Promise.all(mapToLockedEm)
    }).then(adIdsToDelete  => {
      // In this context the adIdsToDelete will be filled
    });
    .catch(error => console.log(error))

  cron.schedule("* * * * *", function() {
  console.log("running a task every minute => delete locked ads");
  });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM