2016-04-21 3 views
-1

Wenn ich eine einfache for-Schleife verwenden, um auf Array-Werte zuzugreifen, geht die Indexvariable verloren und kann daher nicht auf Array zugreifen. Die Verwendung der Indexnummer anstelle der Variablen funktioniert, aber nicht variabel. Dies ist der nervigste Code aller Zeiten.Node JS verliert For-Loop-Indexvariable

/* jshint esnext: true, asi: true */ 
var neo4j = require('node-neo4j') 


// Create the neo4J object 
var db = new neo4j(serverURI) 

exports.addPerson = (body, callback) => { 

if (body.skills) { 
    var sentSkills = body.skills 
    var arraySkills = sentSkills.split(',') 
}else { 
    var sentSkills = [] 
} 

const sentName = body.name 
const sentEmail = body.email 
const sentUsername = body.username 
const sentPassword = body.password 
const lecturerStatus = body.lecturer 

db.readNodesWithLabelsAndProperties('Person',{ email: sentEmail }, function (err, node) { 
    if (err) { 
    return console.log(err) 
    } 
    if (node.length > 0){ 
    // The user already exists 
    callback({code:401,status:'failed',message:'Person already exsits with the name '+sentName,data:sentName}) 
    } 
    else { 
    // Insert new Person 


    db.insertNode({ 
     name: sentName, 
     email: sentEmail, 
     skills: sentSkills, 
     username: sentUsername, 
     password: sentPassword, 
     lecturer: lecturerStatus 
    }, 'Person', function (err, node) { 
    personNode = node 
    if (err) { 
     return console.log(err+1) 
    } 
    else { 


    // I hate you for not working 
    // The i = 0 variable is not accessible -> arraySkill[i]^.trim() 
    // ERROR: cannot read property trim of undefined 
     console.log("success") 
     for (i = 0; i < arraySkills.length; i++){ 
     arraySkills = body.skills.split(',') 
     db.cypherQuery("MATCH (s:Skill {name:'"+arraySkills[i].trim()+"'}) RETURN s", function(err, node){ 
      if (err){ 
      console.log("ERROR1") 
      console.log(err) 
      } 
      else { 
      console.log(arraySkills[0]) 
      if (node.data == '') 
      { 
       db.cypherQuery("CREATE (s:Skill {name:'"+arraySkills[i].trim()+"'}) RETURN s", function(err, node){ 
       if (err){ 
        console.log("ERROR2") 
        console.log(err) 
       } 
       else { 
        console.log(node) 
        db.cypherQuery("MATCH (p:Person), (s:Skill) WHERE p.name = '"+sentName.trim()+"' AND s.name = '"+arraySkills[i].trim()+"' CREATE (p)-[r:knows]->(s) RETURN r", function(err, node){ 
        if (err){ 
         console.log("ERROR3") 
         console.log(err) 
        } 
        else { 
         console.log(node) 
         console.log("Success") 
        } 
        }) 
       } 
       }) 
      } 
      } 
     }) 
     }; 

    } 


     }) 



    // Output node data. 
    callback({code:201,status:'success',message:'Person Added In '+sentName+' found...',data:node}) 

    } 
}) 
} 
+0

Es ist definitiv ein lästiges Problem, aber wenn Sie die Frage lesen, die ich verknüpfte, sehen Sie, dass es eine einfache Lösung hat. –

+0

Keine Notwendigkeit für solche Wut .. gehen Sie eine Pause und entspannen Sie sich ein wenig. –

+0

Hallelujah, es war eine einfache Lösung. Ich werde den Schmerz nicht vergessen. –

Antwort

0

Das ist ein Schließungsproblem, um es zu beheben, müssen Sie Ihren $ http Anruf zu einer neuen Funktion wie diesem bewegen.

for (i = 0; i < arraySkills.length; i++){ 
    var skills = body.skills.split(','); 
    dbQuery(skills,i); // In this function you have to write all the stuff you got under arraySkills = body.skills.split(',') 

}