我知道还有多个其他类似标题的问题,但是没有一个回答我的疑问。
我正在运行一个React应用,并从后端(nodejs + Mysql)获取一些数据。 当我运行我的应用程序时,会发生此错误:-
TypeError: undefined is not iterable (cannot read property
Symbol(Symbol.iterator))
handleReq函数中的。这是代码:-
class Requests extends Component {
constructor(props) {
//state and other stuff
this.handleReq = this.handleReq.bind(this);
}
handleReq(jsonInput) {
let response, expectedResponse, diff, same;
[response, expectedResponse, diff, same] = getResponses(jsonInput[1], jsonInput[2], parseResponse);
console.log(response);
//doing something with variables
}
render(){
return(
//something
);
}
}
function getResponses(gateVal, reqType, callback) {
fetch(`http://localhost:5000/api/getReply/${gateVal}/${reqType}`, {
})
.then(data => data.json())
.then((res) => {
var retData = res.data["replies"];
console.log(retData);
retData = JSON.parse(retData);
let expectedResponse, diff, same;
[expectedResponse, diff, same] = callback(gateVal, reqType, retData);
console.log(expectedResponse);
console.log(same);
return [retData, expectedResponse, diff, same];
});
}
现在,我在VSCode上使用Chrome运行调试器,发现恰好在get Response函数返回的那一点上存在问题。当我将getResponses返回为:-
var returns = getResponses(jsonInput[1], jsonInput[2], parseResponse);
response = returns[0];
expectedResponse = returns[1];
diff = returns[2];
same = returns[3]
它显示退货未定义。 类似地,在getResponses函数中,从回调返回后没有任何运行。
console.log(expectedResponse);
console.log(same);
行根本就不运行。
如何解决此问题?
答案 0 :(得分:2)
您应该从函数中显式public object ThisIsProperty1{get;set;}
public object Property2{get;set;}
,否则函数将返回return
undefined
使用function getResponses(gateVal, reqType, callback) {
//explicitly using return
return fetch(`http://localhost:5000/api/getReply/${gateVal}/${reqType}`, {
})
.then(data => data.json())
.then((res) => {
var retData = res.data["replies"];
console.log(retData);
retData = JSON.parse(retData);
let expectedResponse, diff, same;
[expectedResponse, diff, same] = callback(gateVal, reqType, retData);
console.log(expectedResponse);
console.log(same);
return [retData, expectedResponse, diff, same];
});
}
会更好。
async-await