【发布时间】:2017-07-08 17:46:14
【问题描述】:
我正在尝试使用需要在另一个函数中重新运行的变量运行 FOR 循环,我需要将它们从第一个函数中编码出来, 但是函数将它们接收为“未定义”。
当变量在 collectionChecking 函数中是本地的,除了 ledTypes2 时,当handleResponse2 的函数试图调用他时,hostIndx2 的变量是未定义的。此函数应将响应呈现为 HTML 页面内 ledTypes2 方法的图标。
var fetch = require('node-fetch');
var ledTypes = {
green: "<img id='logo' src='green.png' height='30' width='30'>",
red: "<img id='logo' src='red.png' height='30' width='30'>",
yellow: "<img id='logo' src='yellow.png' height='30' width='30'>"
};
var hosts2 = ['http://host1.com','host2.com','host3.com','host4.com'];
var hostIndx2 = 0;
var lengthVal = hosts2.length;
var token = '1213232431';
function collectionChecking() {
console.log("im inside the func" + hostIndx2 + "---" + lengthVal);
for (; hostIndx2 < lengthVal; hostIndx2++) {
console.log(hostIndx2);
let url = hosts2[hostIndx2];
// sendReq();
fetch(url , {method: 'GET', headers:{"X-AUTH-TOKEN": token, "Content-Type": "text/plain"}, timeout: 30000}
).then(function (res, hostIndx2) {
console.log(res.status, hostIndx2);
handleLedResponse2(res, hostIndx2);
});
}
}
function handleLedResponse2(res, hostIndx2) {
var curSpan = document.getElementById('col_host_' + hostIndx2);
console.log("IM HERE" + res.status + "---" + hostIndx2);
if (res.status === 200 || res.status === 204) {
curSpan.innerHTML = ledTypes.green;
} else if (res.status === 500 || res.status === 404) {
curSpan.innerHTML = ledTypes.red;
} else if (res.status === 300 || res.status === 301 || res.status === 302) {
curSpan.innerHTML = ledTypes.yellow;
}
}
【问题讨论】:
-
请更具体地说明您遇到的问题。哪些确切的变量是未定义的,在哪一行代码上?而且,“需要将它们从第一个函数中编码出来”是什么意思?请更具体地说明您遇到的问题以及您想要实现的目标。
-
谢谢,handleResponse2 的函数试图调用他时,hostIndx2 的变量未定义。此函数应将响应呈现为 HTML 页面内 ledTypes2 方法的图标。
标签: javascript node.js rest loops variables