繁体   English   中英

NodeJS的承诺,递归,异步续

[英]NodeJS Promises, Recursion, Asynchronous Continued

@lyjackal在这里帮助我(NodeJS 异步和递归 )解决了寻找孩子的递归问题。

我仍在努力弄清nodejs的承诺。 我的问题与上一篇文章类似,但略有不同。 我觉得这段代码很接近,但是没有用。

我有一个分配,并且分配具有对路径的引用。 该路径以及所有路径可能有也可能没有父路径。 我需要此代码,以便按原样查找路径的路径,并将每个路径的一些属性堆叠在数组中。 最后的数组如下所示:

[ [0]=>{field: name, match: match_type, value:value},
  [1]=>{field: name, match: match_type, value:value},
  ...]

数组的顺序甚至无关紧要。 这是带有Promises ...的代码,该代码已损坏:

exports.assignRecursiveQuery = function(req,res) {
    var methods = {}
    var query_to_save = []

    methods.recursiveParents = function(path) {
        return new Promise(function(resolve, reject) {
          Path.find({
            _id: path.parent
          }).exec(function(err, parentPaths) {
            Promise
              .all(parentPaths.map(function(parentPath) {
                return methods.recursiveParents(parentPath) 
              }))
              .then(function(promisedPaths) {
                return resolve(promisedPaths);
              });
          });
        });
      }

    Path.find({_id: req.assignment.path_end}).exec(function(err,lastPaths) {
        //add first query rule
        console.log(lastPaths[0]);
        query_to_save.push({field:lastPaths[0].field.api,match:lastPaths[0].match,value:lastPaths[0].value})

        Promise.all(lastPaths.map(function(path) {
          return methods.recursiveParents(path);
        })).then(function(resolvedPaths) {
            for( var x=0; x<resolvedPaths.length; x++ ) {
                console.log(resolvedPaths[x])
                query_to_save.push({field:resolvedPaths[x].field.api,match:resolvedPaths[x].match,value:resolvedPaths[x].value});
            }
            console.log(query_to_save);
            res.jsonp(req.assignment)
        });
    });
}

我可以使用以下代码按需运行它,不确定是否是“正确的方式”,但它确实按预期运行。

 exports.assignRecursiveQuery = function(req, res, next) { var query_to_save = [] var methods = {}; methods.recursiveChildren = function(path) { var item_to_save = { field: path.field.api, match: path.match, value: path.value } query_to_save.push(item_to_save); return new Promise(function(resolve, reject) { Path.find({_id: path.parent}).exec(function(err, parentPath) { Promise .all(parentPath.map(function(parent) { /* collect a promise for each child path this returns a promise */ return methods.recursiveChildren(parent); })) .then(function(resolvedPaths) { /* the top level promise */ resolve(); }); }); }); } Path.find({_id:req.assignment.path_end}).exec(function(err, paths) { Promise.all(paths.map(function(path) { return methods.recursiveChildren(path); })).then(function(resolvedPaths) { var assignment = req.assignment; assignment.assign_query = query_to_save; assignment.save(function(err) { if (err) { return res.status(400).send({ message: errorHandler.getErrorMessage(err) }); } else { req.assignment = assignment; next(); } }); }); }); }; 

暂无
暂无

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

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