-1

When I use a simple for loop to access array values, the index variable gets lost therefore unable to access array. Using index number instead of variable works but not variable. This is the most annoying code ever.

/* 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})

  }
})
}
Brad Larson
  • 170,088
  • 45
  • 397
  • 571
Le Fem
  • 99
  • 1
  • 11

1 Answers1

0

That's a closure problem, to fix it you have to move your $http call to a new function like this.

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(',')

}
QoP
  • 27,388
  • 16
  • 74
  • 74