Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/383.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 对于在环不';不要把每一件物品都检查一遍_Javascript_For Loop - Fatal编程技术网

Javascript 对于在环不';不要把每一件物品都检查一遍

Javascript 对于在环不';不要把每一件物品都检查一遍,javascript,for-loop,Javascript,For Loop,我将Node.js与Express一起用于后端服务器。我以json的形式发送一些数据,在解析之后,我尝试遍历对象,以便分配值。对于一个测试,我将传入一个包含50个对象的对象 我曾尝试将for-in循环与hasOwnProperty一起使用,但从未完成所有操作 for (i in req.body.deviceObject) { if (req.body.deviceObject.hasOwnProperty.call(req.body.deviceObject[i].dId, i)) {

我将Node.js与Express一起用于后端服务器。我以json的形式发送一些数据,在解析之后,我尝试遍历对象,以便分配值。对于一个测试,我将传入一个包含50个对象的对象

我曾尝试将for-in循环与hasOwnProperty一起使用,但从未完成所有操作

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个。

const deviceObject={
1: {
rId:'e43aebb5-234f-4aa6-a666-90179df767bc',
e164:‘449bc7cc-90fa-4b9e-b4c1-1223d825d545’,
uName:'服务器',
uPassword:'管理员',
VCSID:'54191576-47ea-4055-8ea4-bc201dc54f6d',
IP地址:“1.1.1.1”,
dId:'b6178041-86cc-4959-9155-54ca419083e7',
},
35: {
rId:'dce82b00-fa1e-46b8-a3f6-1a5af45175de',
e164:'7cc8190b-c261-40f8-9f62-408f7e8b2450',
uName:“接入点”,
uPassword:'管理员',
VCSID:'3e3e447c-b9fe-4997-ba54-225175b0a84b',
IP地址:“1.1.1.1”,
dId:‘9c97d5a5-b26f-492e-ba5b-bdd33eb3cb30’,
},
};
常量custName='dummy';//req.body.customerName;
常量数组=[];
Object.key(deviceObject).forEach((r)=>{
const text='插入VCS(ip地址、VCS名称、用户名、用户名密码、ID)值($1、$2、$3、$4、$5)';
const vals=设备对象[r];
常量值=[vals.ipAddress、custName、vals.uName、vals.uPassword、vals.VCSID];
常量对象={
文本,
价值观
};
阵列推送(obj);
});
console.log(数组);
const deviceObject={
1: {
rId:'e43aebb5-234f-4aa6-a666-90179df767bc',
e164:‘449bc7cc-90fa-4b9e-b4c1-1223d825d545’,
uName:'服务器',
uPassword:'管理员',
VCSID:'54191576-47ea-4055-8ea4-bc201dc54f6d',
IP地址:“1.1.1.1”,
dId:'b6178041-86cc-4959-9155-54ca419083e7',
},
35: {
rId:'dce82b00-fa1e-46b8-a3f6-1a5af45175de',
e164:'7cc8190b-c261-40f8-9f62-408f7e8b2450',
uName:“接入点”,
uPassword:'管理员',
VCSID:'3e3e447c-b9fe-4997-ba54-225175b0a84b',
IP地址:“1.1.1.1”,
dId:‘9c97d5a5-b26f-492e-ba5b-bdd33eb3cb30’,
},
};
常量custName='dummy';//req.body.customerName;
常量数组=[];
Object.key(deviceObject).forEach((r)=>{
const text='插入VCS(ip地址、VCS名称、用户名、用户名密码、ID)值($1、$2、$3、$4、$5)';
const vals=设备对象[r];
常量值=[vals.ipAddress、custName、vals.uName、vals.uPassword、vals.VCSID];
常量对象={
文本,
价值观
};
阵列推送(obj);
});
console.log(数组);

所以这不是一个很好的答案,但我希望这将有助于您的调试

如果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)
    }
}

所以这不是一个很好的答案,但我希望这将有助于您的调试

如果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)
    }
}


你能把你的循环附加到数据结构上吗?你想循环对象上的属性吗?如果是这样的话,看看这第35个物体有什么不同吗?尝试回显与if-statement不匹配的deviceObject。For循环通常没有理由不遍历所有对象。在迭代过程中应用程序是否挂起?此外,count+=1在if语句中,因此它不能保证计数将递增。在for循环中的if语句外添加另一个变量(totalcount)以递增,以检查是否所有项都已迭代。@kSp我编辑了原始项,使其通过。是否可以附加循环的数据结构?是否尝试循环对象上的属性?如果是这样的话,看看这第35个物体有什么不同吗?尝试回显与if-statement不匹配的deviceObject。For循环通常没有理由不遍历所有对象。在迭代过程中应用程序是否挂起?此外,count+=1在if语句中,因此它不能保证计数将递增。在for循环中的if语句外添加另一个变量(totalcount)以递增,以检查是否所有项都已迭代。@kSp我编辑了原始项,使其通过。您不能像我想的那样在对象上使用.length。@abeldattsun您不能。如果数据不是数组,那么第一个
if
语句会抛出一个错误,当然,您的不是数组。我以为它在数组中(
[]
)formPaste您的输入数据和预期结果数据结构包含在问题的代码块底部。您不能在像我想的那样的对象上使用.length。@abeldattsun您不能。如果数据不是数组,那么第一个
if
语句会抛出一个错误,当然,你的不是数组