当我使用简单的for循环访问数组值时,索引变量会丢失,因此无法访问数组。使用索引号而不是变量可以工作但不可变。这是有史以来最烦人的代码。
/* 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 :(得分:0)
这是一个关闭问题,修复它你必须将你的$ http调用移动到这样的新函数。
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(',')
}