Javascript 循环索引变量的节点JS丢失
当我使用简单的for循环访问数组值时,索引变量丢失,因此无法访问数组。使用索引号代替变量可以工作,但不能使用变量。这是有史以来最烦人的代码Javascript 循环索引变量的节点JS丢失,javascript,node.js,Javascript,Node.js,当我使用简单的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
/* 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})
}
})
}
/*jshint-esnext:true,asi:true*/
var neo4j=require('node-neo4j')
//创建neo4J对象
var db=new neo4j(serverURI)
exports.addPerson=(正文,回调)=>{
if(身体技能){
var sentSkills=body.skills
var arraySkills=sentSkills.split(',')
}否则{
var sentSkills=[]
}
const sentName=body.name
const sentmail=body.email
const sentUsername=body.username
const sentPassword=body.password
const讲师status=body.讲师
db.readNodesWithLabelsAndProperties('Person',{email:sentEmail},函数(err,节点){
如果(错误){
返回console.log(错误)
}
如果(节点长度>0){
//用户已存在
回调({code:401,状态:'failed',消息:'Person已存在,名称为'+sentName,数据为:sentName})
}
否则{
//插入新人
db.insertNode({
姓名:sentName,,
电子邮件:sentEmail,
技能:sentSkills,
用户名:sentUsername,
密码:sentPassword,
讲师:讲师Status
},“人员”,功能(错误,节点){
personNode=node
如果(错误){
返回console.log(err+1)
}
否则{
//我恨你不工作
//i=0变量不可访问->arraySkill[i]^.trim()
//错误:无法读取未定义的属性修剪
console.log(“成功”)
对于(i=0;i(s)RETURN r”,函数(err,node){
如果(错误){
控制台日志(“错误3”)
console.log(错误)
}
否则{
console.log(节点)
console.log(“成功”)
}
})
}
})
}
}
})
};
}
})
//输出节点数据。
回调({code:201,状态:'success',消息:'Person Added In'+sentName+'found…',数据:node})
}
})
}
这是一个闭包问题,要解决它,您必须将$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(',')
}
for(i=0;i
这肯定是一个恼人的问题,但如果你阅读我链接的问题,你会发现它有一个简单的解决方案。没有必要这么生气。。去休息一下,放松一下。哈利路亚,这是一个简单的解决办法。我不会忘记痛苦。