Javascript 即使在console.log中显示数据后,Reactjs对象数据仍显示未定义
当我运行此代码时,它应该呈现Javascript 即使在console.log中显示数据后,Reactjs对象数据仍显示未定义,javascript,reactjs,Javascript,Reactjs,当我运行此代码时,它应该呈现 Hello World! 但它使 Loading... 这是我的密码: import React, { Component } from 'react'; class GroupsHomePage extends Component { constructor() { super() this.state={ groupInfo:[] } } componentDi
Hello World!
但它使
Loading...
这是我的密码:
import React, { Component } from 'react';
class GroupsHomePage extends Component {
constructor() {
super()
this.state={
groupInfo:[]
}
}
componentDidMount(){
let store = JSON.parse(localStorage.getItem('login'))
var url = 'http://127.0.0.1:8000/myapi/groupsdata/'+this.props.groupId+'/?format=json'
fetch(url,{
method:'GET',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Token '+store.token,
},
})
.then(res=>res.json().then(result=>{
this.setState({groupInfo: result})
}))
}
render() {
console.log(this.state.groupInfo)
if(this.state.groupInfo.length){
return (
<div>
<p>Hello World!</p>
</div>
);
}else{
return(
<div className='container'>
<p>Loading...</p>
</div>
)
}
}
}
export default GroupsHomePage;
import React,{Component}来自'React';
类GroupsHomePage扩展组件{
构造函数(){
超级()
这个州={
群组信息:[]
}
}
componentDidMount(){
let store=JSON.parse(localStorage.getItem('login'))
var url='1〕http://127.0.0.1:8000/myapi/groupsdata/“+this.props.groupId+”/?格式=json”
获取(url{
方法:'GET',
标题:{
“内容类型”:“应用程序/json”,
“授权”:“令牌”+store.Token,
},
})
.then(res=>res.json()。然后(result=>{
this.setState({groupInfo:result})
}))
}
render(){
console.log(this.state.groupInfo)
if(this.state.groupInfo.length){
返回(
你好,世界
);
}否则{
返回(
加载
)
}
}
}
导出默认的GroupsHomePage;
当我运行代码时,在控制台中显示:
[]
{id:6,名称:“ffff”,成员:0,管理员:{id:7,名字:“test6”,姓氏:“test6”}
为什么显示加载…而不是Hello World?试试这个。
console.log(typeof(this.state.groupInfo))
我认为groupInfo属于对象类型
所以在控制台中,如果您看到上线对象的输出,那么将if条件更改为
if(this.state.groupInfo){
你也不应该使用
if(this.state.groupInfo.length){
如果您已将groupInfo定义为[]。
因为它将为您提供true,我认为您只希望在groupInfo数组中有一些数据时运行此if条件
if(this.state.groupInfo.length !== 0){
这是个好办法,试试这个。
console.log(typeof(this.state.groupInfo))
我认为groupInfo属于对象类型
所以在控制台中,如果您看到上线对象的输出,那么将if条件更改为
if(this.state.groupInfo){
你也不应该使用
if(this.state.groupInfo.length){
如果您已将groupInfo定义为[]。
因为它将为您提供true,我认为您只希望在groupInfo数组中有一些数据时运行此if条件
if(this.state.groupInfo.length !== 0){
这是一个好方法看起来没问题,API可能会骗你 在检查
长度之前
if(this.state.groupInfo.length){
...
}
确保结构
groupInfo
是对象的array
。看起来正常,API可能会欺骗您
在检查长度之前
if(this.state.groupInfo.length){
...
}
确保结构groupInfo
是对象的array
。您正在检查“groupInfo”是否具有length
属性。此属性通常与数组或字符串类型关联。在本例中,我认为“groupInfo”是一种对象类型,因此它没有这个length
属性。我建议更改条件并检查“groupInfo”是否具有id
属性,例如:
if (this.state.groupInfo.id) {
...
}
另一种方法是在数组中转换该对象并检查其长度,如下所示:
if (Object.keys(this.state.groupInfo).length) {
...
}
您正在检查“groupInfo”是否具有length
属性。此属性通常与数组或字符串类型关联。在本例中,我认为“groupInfo”是一种对象类型,因此它没有这个length
属性。我建议更改条件并检查“groupInfo”是否具有id
属性,例如:
if (this.state.groupInfo.id) {
...
}
另一种方法是在数组中转换该对象并检查其长度,如下所示:
if (Object.keys(this.state.groupInfo).length) {
...
}
所以首先,默认的groupInfo状态应该是构造函数中的空对象,如下所示 groupInfo:{} 检查你的“如果”情况是这样的
if(Object.keys(this.state.groupInfo).length > 0)
此条件用于不同于数组的JSON对象键,因此首先,默认的groupInfo状态应该是构造函数中的空对象,如下所示 groupInfo:{} 检查你的“如果”情况是这样的
if(Object.keys(this.state.groupInfo).length > 0)
此条件用于不同于数组的JSON对象键