javascript - For in 循环不会遍历对象的每个项目
问题描述
我正在使用带有 Express 的 Node.js 作为后端服务器。我将一些数据作为 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 个。
解决方案
所以这不是一个完整的答案,但我希望这将有助于您的调试。
如果您的 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)
}
}
推荐阅读
- sql - 多个 ID 在同一列中的查找名称
- apache-camel - 我如何支持 camel-as2 服务器的不同合作伙伴关系
- plot - 无法将 Float64 转换为系列数据以在 Julia 中绘图
- google-cloud-platform - 获取 google 服务帐户的已签名 Jwt 令牌
- python - 如何使用 pandas 根据另一列查找金额的前 3 个值
- javascript - MongoDB 不包含字段,但发送请求时会显示此字段
- azure - 可以在 Databricks 中执行 Azure Key Vault 加密解密吗?
- android - SAF(存储访问网络)自定义选项
- griddb - 如何使用 matplotlib 为存储在 GridDB 容器中的推文数据绘制词云?
- docker - 在 Dockerfile 中更改 `/etc/passwd` 的权限