当我尝试在 JavaScript 中将变量从一个函数传递到另一个函数时,我收到引用错误:变量未定义错误(邮递员)
错误:ReferenceError:isinDB 未定义
//Verify ISIN with DB API
pm.test("Row : " + i + "-"+dataArr[1] + ":Verify symbol isin with DB API", function () {
console.log("isin " + dataArr[6]);
//Getting isin from DB API
pm.sendRequest("http://192.168.xx.xxx:8080/ords/unidata/symbol/symbol/" + dataArr[1], function (err, response ) {
var resBoday = response.json()
var isinDB = resBoday.items[0].isin_code;
console.log("isinDB "+isinDB);
});
pm.expect(dataArr[6]).is.to.equals(isinDB);
});
答案 0 :(得分:0)
isinDB
是在函数回调中声明的,所以没有在外部作用域中定义。即使您在外部作用域中声明它,它也将不可用,直到调用带有 response
的回调函数。也许您可以通过这种方式在回调中进行测试?
//Verify ISIN with DB API
pm.test("Row : " + i + "-"+dataArr[1] + ":Verify symbol isin with DB API", function () {
console.log("isin " + dataArr[6]);
//Getting isin from DB API
pm.sendRequest("http://192.168.xx.xxx:8080/ords/unidata/symbol/symbol/" + dataArr[1], function (err, response ) {
var resBoday = response.json()
var isinDB = resBoday.items[0].isin_code;
console.log("isinDB "+isinDB);
pm.expect(dataArr[6]).is.to.equals(isinDB);
});
});
答案 1 :(得分:0)
问题已从以下代码修复..感谢支持
//Verify ISIN with DB API
pm.test("Row : " + i + " - " + dataArr[1] + " : Verify symbol isin with DB API", function () {
var isinC = dataArr[6];
console.log("isin " + dataArr[6]);
//Getting isin from DB API
pm.sendRequest("http://192.168.xx.xxx:8080/ords/unidata/symbol/symbol/" + dataArr[1], function (err, response) {
var resBoday = response.json()
var isinDB = resBoday.items[0].isin_code;
console.log("isinDB " + isinDB);
//pm.expect(dataArr[6]).is.to.equals(isinDB);
console.log("test" + isinC);
pm.expect(isinC).is.to.equals(isinDB);
});
});