嵌套异步瀑布上下文中的Javascript变量范围

时间:2014-04-08 21:44:38

标签: javascript node.js scope async.js waterfall

我试图在我的嵌套编程中更加清晰,但我遇到了aync.waterfall的问题。我想知道是否有其他人遇到过类似的事情。我有以下代码正确打印出节点对象,但不允许我访问各个变量。第17行的控制台输出如下所示: 找到现有节点= [{lastName:' Anonymous',     emailAddress:' myname@mydomain.com',     formattedName:'亨利匿名',     mainAddress:' 2016 Nowhere Dr \ nSnogsville,TX 11121',     firstName:' Henry',     _id:0}] undefined

我很困惑如何控制台可以访问各个变量,但我不能在我的代码中。任何建议都会非常受欢迎。这是代码:

function getNode(nodeLabels, nodeProperties, getNodeCallback) {
    async.waterfall([
        function(nextCallback) {
            db.readNodesWithLabelsAndProperties(nodeLabels, nodeProperties, function (err, nodesRead) {
                nextCallback(err ? err : null
                           , (nodesRead.length < 1)
                           , (nodesRead.length < 1) ? null : nodesRead);
                });
        },

        function(createNode, existingNode, nextCallback) {
            if (createNode) {
                console.log('Creating node ...');
                db.insertNode(nodeProperties, nodeLabels, nextCallback);
            } else {
                console.log('Found existing node = ', existingNode, existingNode._id);
                nextCallback(null, existingNode);
            }
        }
    ], getNodeCallback);
}

function getPersonNode(userToGet, getPersonNodeCallback) {
    console.log(userToGet);

    getNode('Person', { firstName:     userToGet._json.firstName
                      , lastName:      userToGet._json.lastName
                      , formattedName: userToGet._json.formattedName
                      , emailAddress:  userToGet._json.emailAddress
                      , mainAddress:   userToGet._json.mainAddress
                      }
          , getPersonNodeCallback);
 }

function createRelationship(startNodeId, endNodeId, relationshipType, relationshipProperties, createRelationshipCallback) {
    console.log('Creating relationship between ', startNodeId, ' and ', endNodeId);

    async.waterfall([
        function(nextCallback) {
            db.readTypedRelationshipsOfNode(startNodeId, [relationshipType], function(err, relationshipsRead) {
                console.log('Relationships of type = ', relationshipType, ' = ', relationshipsRead);
//              nextCallback(err ? err : null
//                         , !relationshipsRead
//                         , relationshipsRead ? relationshipsRead : null);
                if (err) {
                    console.log('Error trying to find existing relationship = ', err);
                    nextCallback(err, false, null);
                } else {
                    if (!relationshipsRead) {
                        console.log('Relationship does not exist, create it');
                        nextCallback( null, true, null);
                    } else {
                        console.log('Relationship already exists, do not create it');
                        nextCallback( null, false, relationshipsRead);
                    }
                }
            });
        },

        function(createRelationship, existingRelationship, nextCallback) {
            if (createRelationship) {
                db.insertRelationship(startNodeId, endNodeId, relationshipType, relationshipProperties, nextCallback);
            } else {
                nextCallback(null, existingRelationship);
            }
        }
    ], createRelationshipCallback);
}

function createAssociationRelationship(associationName, personNode, createAssociationRelationshipCallback) {
    async.waterfall([
        function(nextCallback) {
            getNode('Association', {name: associationName}, function(err, associationNode) {
                if (err) {
                    console.log('Error getting association node = ', err);
                    nextCallback(err, null);
                } else {
                    console.log('Got association node = ', associationNode);
                    nextCallback(null, associationNode);
                }
            });
        },

        function(assocNode, nextCallback) {
            console.log('Creating relationship between ', personNode._id, ' and ', assocNode._id);
            createRelationship(personNode._id, assocNode._id, 'Is Member Of', null, function(err, normalRelationship) {
                if (err) {
                    console.log('Error creating association relationship = ', err);
                    nextCallback(err, null);
                } else {
                    console.log('Created Is Member Of Relationship = ', normalRelationship);
                    nextCallback(null, assocNode);
                }
            });
        },

        function(assNode, nextCallback) {
            createRelationship(assNode._id, personNode._id, 'Has Member', null, function(err, reverseRelationship) {
                if (err) {
                    console.log('Error creating association relationship = ', err);
                    nextCallback(err, null);
                } else {
                    console.log('Created Has Member Relationship = ', reverseRelationship);
                    nextCallback(null, assNode);
                }
            });
        }
    ], createAssociationRelationshipCallback);
}

app.get('/', function(req, res){
  if (typeof req.user != 'undefined') {
      async.waterfall([
          function(nextCallback) {
              getPersonNode(req.user, function(err, newPersonNode) {
                if (err) {
                    console.log('Error = ', err);
                    nextCallback(err);
                }
                console.log("Node = ", newPersonNode);
                nextCallback(null, newPersonNode);
              });
        },

        function(startNode, nextCallback) {
            req.user._json.associations.split(',').forEach(function(nameValue, index, arrayItem) {
                createAssociationRelationship(nameValue, startNode, function(err, associationRelationship) {
                    if (err)
                        console.log('Error = ', err);
                    else
                        console.log('Created relationship between ', startNode, ' and ', associationRelationship);
                });
            });
        }
    ]);
  }                                         
  res.render('index', { user: req.user });
});

0 个答案:

没有答案