Javascript 从api获取数据时未显示数据
首先,我在states内部声明了一个empy数组: 但数据没有显示,而是显示了一些错误: 此外,将状态设置为时也会显示错误Javascript 从api获取数据时未显示数据,javascript,node.js,reactjs,mongodb,ecmascript-6,Javascript,Node.js,Reactjs,Mongodb,Ecmascript 6,首先,我在states内部声明了一个empy数组: 但数据没有显示,而是显示了一些错误: 此外,将状态设置为时也会显示错误 Unhandled Rejection (TypeError): undefined is not an object (evaluating 'this.setState') 它指向设定状态线 有人能解决这个问题吗?我不明白你为什么在这里使用JSON.stringify return JSON.stringify(jsonData); 因为您已经在这里将数据转换为js
Unhandled Rejection (TypeError): undefined is not an object (evaluating 'this.setState')
它指向设定状态线
有人能解决这个问题吗?我不明白你为什么在这里使用JSON.stringify
return JSON.stringify(jsonData);
因为您已经在这里将数据转换为json
return response.json();
调用API获取数据的另一个方法是使用componentDidMount生命周期挂钩。尝试像这样更改代码
componentDidMount() {
fetch('http://localhost:3000/"my end point in api"')
.then(function(response) {
return response.json();
})
.then(function(jsonStr) {
this.setState({ CourseTitle: jsonStr.course_title });
});
}
我不明白你为什么在这里使用JSON.stringify
return JSON.stringify(jsonData);
因为您已经在这里将数据转换为json
return response.json();
调用API获取数据的另一个方法是使用componentDidMount生命周期挂钩。尝试像这样更改代码
componentDidMount() {
fetch('http://localhost:3000/"my end point in api"')
.then(function(response) {
return response.json();
})
.then(function(jsonStr) {
this.setState({ CourseTitle: jsonStr.course_title });
});
}
根据官方文件,componentWillMount已贬值,请尝试在componentDidMount中调用它
componentDidMount(){
fetch('http://localhost:3000/"my end point in api"')
.then(function(response) {
return response.json();
}).then(function(jsonData) {
return JSON.stringify(jsonData);
})
.then(function(jsonStr) {
this.setState({ CourseTitle: jsonStr.course_title });
alert(jsonStr.course_title);
});
}
在打印时,设置如下条件:
console.logprinting,this.state.CourseTile?this.state.CourseTile:尚未提取根据官方文档,componentWillMount已折旧,请尝试在componentDidMount中调用它
componentDidMount(){
fetch('http://localhost:3000/"my end point in api"')
.then(function(response) {
return response.json();
}).then(function(jsonData) {
return JSON.stringify(jsonData);
})
.then(function(jsonStr) {
this.setState({ CourseTitle: jsonStr.course_title });
alert(jsonStr.course_title);
});
}
在打印时,设置如下条件:
console.logprinting,this.state.CourseTile?this.state.CourseTile:尚未获取未获取为什么您获取的是post中提到的内容而不是这样
componentDidMount() {
fetch('http://localhost:3000/"my end point in api"')
.then(function(response) {
return response.json();
})
.then(function(jsonStr) {
this.setState({ CourseTitle: jsonStr.course_title });
alert(jsonStr.course_title);
});
}
并在渲染之前对数据进行控制台处理
并且建议根据官方文档《不获取》在componentDidMount生命周期方法中调用api。为什么要获取文章中提到的api而不是像这样
componentDidMount() {
fetch('http://localhost:3000/"my end point in api"')
.then(function(response) {
return response.json();
})
.then(function(jsonStr) {
this.setState({ CourseTitle: jsonStr.course_title });
alert(jsonStr.course_title);
});
}
并在渲染之前对数据进行控制台处理
建议在componentDidMount生命周期方法中调用api,请参见三件事的官方文档:
尝试按照其他人的建议,更改组件WillMount by componentDidMount
componentDidMount(){
fetch('http://localhost:3000/"my end point in api"')
.then(function(response) {
return response.json();
}).then(function(jsonData) {
return JSON.stringify(jsonData);
})
.then(function(jsonStr) {
this.setState({ CourseTitle: jsonStr.course_title });
alert(jsonStr.course_title);
});
}
另外,请遵循删除JSON.stringifyjsonData的建议,因为您正在转换字符串中的对象,之后,您将尝试访问对象属性,但这些属性不存在
最后,JavaScript中的一个常见错误是在不同的上下文中误解了它的含义。例如,请看下一个代码段:
常量对象={
说{
console.logthis.toString;
},
赛迪{
setTimeoutfunction{
console.logthis.toString;
}, 500;
}
};
obj.say;
obj.saydayed 三件事:
尝试按照其他人的建议,更改组件WillMount by componentDidMount
componentDidMount(){
fetch('http://localhost:3000/"my end point in api"')
.then(function(response) {
return response.json();
}).then(function(jsonData) {
return JSON.stringify(jsonData);
})
.then(function(jsonStr) {
this.setState({ CourseTitle: jsonStr.course_title });
alert(jsonStr.course_title);
});
}
另外,请遵循删除JSON.stringifyjsonData的建议,因为您正在转换字符串中的对象,之后,您将尝试访问对象属性,但这些属性不存在
最后,JavaScript中的一个常见错误是在不同的上下文中误解了它的含义。例如,请看下一个代码段:
常量对象={
说{
console.logthis.toString;
},
赛迪{
setTimeoutfunction{
console.logthis.toString;
}, 500;
}
};
obj.say;
obj.saydayed;您不需要JSON.stringifyjsonData,因为它将JavaScript对象或值转换为JSON字符串。如果您console.logjsonStr,您就会知道我的意思,然后尝试访问字符串的.course\u标题。这个问题有任何更新吗?我的回答解决了你的问题吗?如果是,请将我的答案标记为aceepted。您不需要JSON.stringifyjsonData,因为它将JavaScript对象或值转换为JSON字符串。如果您console.logjsonStr,您就会知道我的意思,然后尝试访问字符串的.course\u标题。这个问题有任何更新吗?我的回答解决了你的问题吗?如果是,请将我的答案标记为已接受。嘿,托尼,我无法验证你的答案是否有效。我尝试放置console.log,但没有显示任何内容。很抱歉,我想我不能在正确的地方使用console.log。请你编辑你的代码,放入console.log或任何其他方法,以便我可以验证答案。嘿,托尼,我无法验证你的答案是否有效。我尝试放入console.log,但没有显示任何结果。很抱歉,我想我不能在正确的地方使用console.log。你能编辑你的代码吗?输入console.log或任何其他方法,这样我就可以验证答案了