Javascript 未定义RenderItems返回中没有未定义
大家好,我是reactjs的新手,我尝试让响应数据对象能够显示在webapp上供用户查看,而不是让用户查看页面,然后去网络/控制台查看文件上传错误响应name='title',所以我决定使用下面的函数来给我数据响应,我继续获取此错误“未定义渲染项”无未定义 以下是示例代码:Javascript 未定义RenderItems返回中没有未定义,javascript,django,reactjs,django-rest-framework,Javascript,Django,Reactjs,Django Rest Framework,大家好,我是reactjs的新手,我尝试让响应数据对象能够显示在webapp上供用户查看,而不是让用户查看页面,然后去网络/控制台查看文件上传错误响应name='title',所以我决定使用下面的函数来给我数据响应,我继续获取此错误“未定义渲染项”无未定义 以下是示例代码: import React from "react"; import PropTypes from "prop-types"; import axios from 'axios' imp
import React from "react";
import PropTypes from "prop-types";
import axios from 'axios'
import { Card,} from "shards-react";
class Uploaddatastore extends React.Component {
constructor(props) {
super(props);
this.state = {
UploadNotification: []
}
}
componentDidMount(){
let notificationComponent = this;
axios.get("http://127.0.0:8000/uploads/file_upload/")
.then(function (response) {
console.log(response);
console.log(response.data);
notificationComponent.setState({
UploadNotification: response.data.items
});
})
.catch(function (error) {
console.log(error);
});
}
render() {
if (this.state.UploadNotification.length) {
let renderItems = this.state.UploadNotification.map(function(item, i) {
return <li key={i}>{item.title}</li>
});
}
return (
<div>
<span className="text-danger"> {renderItems} </span>
</div>
);
}
};
export default Uploaddatastore;
从“React”导入React;
从“道具类型”导入道具类型;
从“axios”导入axios
从“碎片反应”导入{Card,};
类Uploaddatastore扩展React.Component{
建造师(道具){
超级(道具);
此.state={
上传通知:[]
}
}
componentDidMount(){
让notificationComponent=this;
axios.get(“http://127.0.0:8000/uploads/file_upload/")
.然后(功能(响应){
控制台日志(响应);
console.log(response.data);
notificationComponent.setState({
上载通知:response.data.items
});
})
.catch(函数(错误){
console.log(错误);
});
}
render(){
if(this.state.UploadNotification.length){
让renderItems=this.state.UploadNotification.map(函数(项,i){
return{item.title}
});
}
返回(
{renderItems}
);
}
};
导出默认上传数据存储;
您对renderItems
变量的作用域设置错误。它仅限于if
块。在之前声明,如果
:
render(){
让我们来讨论一下;
if(this.state.UploadNotification.length){
renderItems=this.state.UploadNotification.map(函数(项,i){
return{item.title}
});
}
返回(
{renderItems}
);
}
};
将
设为块范围。如果,则将该语句移到之外。(事实上,您根本不需要if
。)嘿,如果您能帮助使该功能能够在页面上为用户呈现控制台响应,我们将不胜感激。有可能吗?Christian Fritz成功了,谢谢。尽管我希望您能帮助我从网络/控制台获取对象,并在WebApp页面上为用户呈现对象,有可能吗?