javascript - Node JS losing for loop index variable -


this question has answer here:

when use simple loop access array values, index variable gets lost therefore unable access array. using index number instead of variable works not variable. annoying code ever.

/* jshint esnext: true, asi: true */ var neo4j = require('node-neo4j')   // create 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){     // user exists     callback({code:401,status:'failed',message:'person exsits 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 {        // hate not working      // = 0 variable not accessible -> arrayskill[i]^.trim()      // error: cannot read property trim of undefined       console.log("success")       (i = 0; < 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) p.name = '"+sentname.trim()+"' , 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})    } }) } 

that's closure problem, fix have move $http call new function this.

for (i = 0; < arrayskills.length; i++){    var skills = body.skills.split(',');    dbquery(skills,i); // in function have write stuff got under arrayskills = body.skills.split(',')  } 

Comments

Popular posts from this blog

html - Styling progress bar with inline style -

java - Oracle Sql developer error: could not install some modules -

How to use autoclose brackets in Jupyter notebook? -