最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

nodeJS中的循环问题

IT培训 admin 10浏览 0评论

nodeJS中的循环问题

我在nodeJS中有一个循环,需要处理续集查询的结果。我认为必须使用promise或其他方式,因为代码仅针对最后一条记录执行。

代码是:

pages.forEach(function(page) {
    db.Page.findOne({attributes: ['html', 'template', 'permalink'], 
       where: { id: page.id } }).then(obj=> {
            html=obj.html;
            permalink=obj.permalink;
            template=obj.template;
            var variables=[];

            console.log("PERMALINK",permalink);  // FOLLOWING CODE IS RUN ONLY FOR THE LAST RECORD

            db.PageVariable.findAll({
                attributes: ['variable_name','variable_value'],
                raw:true,
                where: {id_page: page.id}
            }).then(function (items) {
                console.log("----------");
                //.......
            }).catch(function (err) {
                console.log(err);
        });
    });
});

如何修改循环以使其正常工作?

回答如下:

尝试将page处理放入返回承诺的函数中,如下所示:

const getPagePromise = page =>
  db.Page.findOne({
    attributes: ['html', 'template', 'permalink'],
    where: {
      id: page.id
    }
  }).then(obj => {
    html = obj.html;
    permalink = obj.permalink;
    template = obj.template;
    var variables = [];

    console.log("PERMALINK", permalink); // FOLLOWING CODE IS RUN ONLY FOR THE LAST RECORD

    return db.PageVariable.findAll({
      attributes: ['variable_name', 'variable_value'],
      raw: true,
      where: {
        id_page: page.id
      }
    }).then(function(items) {
      console.log("----------");
      //.......
    }).catch(function(err) {
      console.log(err);
    });
  });
});

然后将map您的pages数组转换为一个promise数组(就像预先定义的那样),如下所示:

const allPagesPromises = pages.map(getPagePromise);

最后,使用Promise.all来解决/拒绝所有承诺

Promise.all(allPagesPromises)
  .then(allResults => { // Do stuff })
  .catch(err => { // Handle errors });

注意

请注意避免过多的回调嵌套,例如在处理页面时。而是将逻辑分成返回承诺的小函数,这将使事情更易于阅读/调试。

nodeJS中的循环问题

我在nodeJS中有一个循环,需要处理续集查询的结果。我认为必须使用promise或其他方式,因为代码仅针对最后一条记录执行。

代码是:

pages.forEach(function(page) {
    db.Page.findOne({attributes: ['html', 'template', 'permalink'], 
       where: { id: page.id } }).then(obj=> {
            html=obj.html;
            permalink=obj.permalink;
            template=obj.template;
            var variables=[];

            console.log("PERMALINK",permalink);  // FOLLOWING CODE IS RUN ONLY FOR THE LAST RECORD

            db.PageVariable.findAll({
                attributes: ['variable_name','variable_value'],
                raw:true,
                where: {id_page: page.id}
            }).then(function (items) {
                console.log("----------");
                //.......
            }).catch(function (err) {
                console.log(err);
        });
    });
});

如何修改循环以使其正常工作?

回答如下:

尝试将page处理放入返回承诺的函数中,如下所示:

const getPagePromise = page =>
  db.Page.findOne({
    attributes: ['html', 'template', 'permalink'],
    where: {
      id: page.id
    }
  }).then(obj => {
    html = obj.html;
    permalink = obj.permalink;
    template = obj.template;
    var variables = [];

    console.log("PERMALINK", permalink); // FOLLOWING CODE IS RUN ONLY FOR THE LAST RECORD

    return db.PageVariable.findAll({
      attributes: ['variable_name', 'variable_value'],
      raw: true,
      where: {
        id_page: page.id
      }
    }).then(function(items) {
      console.log("----------");
      //.......
    }).catch(function(err) {
      console.log(err);
    });
  });
});

然后将map您的pages数组转换为一个promise数组(就像预先定义的那样),如下所示:

const allPagesPromises = pages.map(getPagePromise);

最后,使用Promise.all来解决/拒绝所有承诺

Promise.all(allPagesPromises)
  .then(allResults => { // Do stuff })
  .catch(err => { // Handle errors });

注意

请注意避免过多的回调嵌套,例如在处理页面时。而是将逻辑分成返回承诺的小函数,这将使事情更易于阅读/调试。

与本文相关的文章

发布评论

评论列表 (0)

  1. 暂无评论