我将Node.js和Express一起用于后端服务器。我以json的形式发送一些数据,解析之后,我试图遍历该对象,以便我可以分配值。为了进行测试,我传入了一个包含50个对象的对象。
我已经尝试过在hasOwnProperty中使用for in循环,但是它从未完成所有操作。
for (i in req.body.deviceObject) {
if (req.body.deviceObject.hasOwnProperty.call(req.body.deviceObject[i].dId, i)) {
newVcsObject[i] = new PQ(
'insert into VCS(ip_address, vcs_name, user_name, user_password, ID) values ($1, $2, $3, $4, $5)'
);
newVcsObject[i].values = [
req.body.deviceObject[i].ipAddress,
req.body.customerName,
req.body.deviceObject[i].uName,
req.body.deviceObject[i].uPassword,
req.body.deviceObject[i].VCSID
];
console.log(i);
count += 1;
}
}
edit: this is my data structure:
"deviceObject": {
"1": {
"rId": "e43aebb5-234f-4aa6-a666-90179df767bc",
"e164": "449bc7cc-90fa-4b9e-b4c1-1223d825d545",
"uName": "Server",
"uPassword": "admin",
"VCSID": "54191576-47ea-4055-8ea4-bc201dc54f6d",
"ipAddress": "1.1.1.1",
"dId": "b6178041-86cc-4959-9155-54ca419083e7"
},
//there are more in between, this is where it's stuck
"35": {
"rId": "dce82b00-fa1e-46b8-a3f6-1a5af45175de",
"e164": "7cc8190b-c261-40f8-9f62-408f7e8b2450",
"uName": "access point",
"uPassword": "admin",
"VCSID": "3e3e447c-b9fe-4997-ba54-225175b0a84b",
"ipAddress": "1.1.1.1",
"dId": "9c97d5a5-b26f-492e-ba5b-bdd33eb3cb30"
}
// goes all the way to 50
我总是输入35/50。我已经仔细检查过,服务器可以正常接收所有50个消息,但是它只会循环访问前35个消息。
答案 0 :(得分:0)
const deviceObject = {
1: {
rId: 'e43aebb5-234f-4aa6-a666-90179df767bc',
e164: '449bc7cc-90fa-4b9e-b4c1-1223d825d545',
uName: 'Server',
uPassword: 'admin',
VCSID: '54191576-47ea-4055-8ea4-bc201dc54f6d',
ipAddress: '1.1.1.1',
dId: 'b6178041-86cc-4959-9155-54ca419083e7',
},
35: {
rId: 'dce82b00-fa1e-46b8-a3f6-1a5af45175de',
e164: '7cc8190b-c261-40f8-9f62-408f7e8b2450',
uName: 'access point',
uPassword: 'admin',
VCSID: '3e3e447c-b9fe-4997-ba54-225175b0a84b',
ipAddress: '1.1.1.1',
dId: '9c97d5a5-b26f-492e-ba5b-bdd33eb3cb30',
},
};
const custName = 'dummy'; // req.body.customerName;
const array = [];
Object.keys(deviceObject).forEach((r) => {
const text = 'insert into VCS(ip_address, vcs_name, user_name, user_password, ID) values ($1, $2, $3, $4, $5)';
const vals = deviceObject[r];
const values = [vals.ipAddress, custName, vals.uName, vals.uPassword, vals.VCSID];
const obj = {
text,
values,
};
array.push(obj);
});
console.log(array);
答案 1 :(得分:0)
所以这不是一个完整的答案,但是我希望这将有助于您的调试。
如果您的for循环已终止,则意味着它已成功遍历每个对象。如果您想知道为什么未正确解析新的VcsObject,那么if语句似乎不会成功。尝试将else语句与日志一起使用,以查看if语句是否在您中发生任何故障,以便说明所有控制逻辑。
for (i in req.body.deviceObject) {
if (req.body.deviceObject.hasOwnProperty.call(req.body.deviceObject[i].dId, i)) {
newVcsObject[i] = new PQ(
'insert into VCS(ip_address, vcs_name, user_name, user_password, ID) values ($1, $2, $3, $4, $5)'
);
newVcsObject[i].values = [
req.body.deviceObject[i].ipAddress,
req.body.customerName,
req.body.deviceObject[i].uName,
req.body.deviceObject[i].uPassword,
req.body.deviceObject[i].VCSID
];
console.log(i);
count += 1;
} else {
// Log when the if-statement fails to execute
console.log("Warning! If statement failed on iteration " + i)
}
}
如果您的for循环从未终止并挂断,则表示您有一些永不返回的函数。我不确定这是否是您的情况,但是您将需要编写多个日志语句以查看失败之处,或者知道哪个函数可能从未返回过。
for (i in req.body.deviceObject) {
console.log("Iteration: " + i)
console.log("checking hasOwnProperty")
if (req.body.deviceObject.hasOwnProperty.call(req.body.deviceObject[i].dId, i)) {
console.log("Assigning a new PQ object")
newVcsObject[i] = new PQ(
'insert into VCS(ip_address, vcs_name, user_name, user_password, ID) values ($1, $2, $3, $4, $5)'
);
console.log("Assigning newVcsObject values")
newVcsObject[i].values = [
req.body.deviceObject[i].ipAddress,
req.body.customerName,
req.body.deviceObject[i].uName,
req.body.deviceObject[i].uPassword,
req.body.deviceObject[i].VCSID
];
console.log(i);
count += 1;
} else {
// Log when the if-statement fails to execute
console.log("Warning! If statement failed on iteration " + i)
}
}