Javascript 未捕获(承诺中)类型错误:无法读取属性';长度';未定义错误的定义
我在尝试应用条件以显示数据库中的文章时遇到此问题 这是my Article.js组件,错误显示在这里Javascript 未捕获(承诺中)类型错误:无法读取属性';长度';未定义错误的定义,javascript,reactjs,Javascript,Reactjs,我在尝试应用条件以显示数据库中的文章时遇到此问题 这是my Article.js组件,错误显示在这里 import React, { Component } from 'react'; import axios from 'axios'; class Articles extends Component { state = { articles: [], status: null } componentWillMount() { this.ge
import React, { Component } from 'react';
import axios from 'axios';
class Articles extends Component {
state = {
articles: [],
status: null
}
componentWillMount() {
this.getArticles();
}
getArticles = () => {
axios.get("https://arthuro-gomez-react.netlify.app/api/articles")
.then(res => {
this.setState({
articles: res.data.articles,
status: 'success'
});
});
}
render() {
if (this.state.articles.length >= 1) {
var listArticles = this.state.articles.map((article) => {
return (
<article className="article-item" id="article-template">
<div className="image-wrap">
<img
src="https://unhabitatmejor.leroymerlin.es/sites/default/files/styles/header_category/public/2018-
10/4%20paisaje%20macedonia.jpg?itok=AELknmF8" alt="Paisaje" />
</div>
<h2>{article.title}</h2>
<span className="date">
{article.date}
</span>
<a href="#">Leer más</a>
<div className="clearfix"></div>
</article>
);
});
return (
<div id="articles">
{listArticles}
</div>
);
} else if (this.state.articles.length === 0 && this.state.status === 'success') {
return (
<div id="articles">
<h2 className="subheader">No hay articulos para mostrar</h2>
<p>Todavia no hay contenido en esta sección</p>
</div>
);
} else {
return (
<div id="articles">
<h2 className="subheader">Cargando</h2>
<img
src="https://pa1.narvii.com/6707/510b0daee67fbc091f14b9d8ef40aeb6c0d4dc7d_hq.gif"/>
</div>
);
}
}
}
export default Articles;
import React,{Component}来自'React';
从“axios”导入axios;
类文章扩展了组件{
状态={
第[…]条,
状态:空
}
组件willmount(){
这个.getArticles();
}
getArticles=()=>{
axios.get(“https://arthuro-gomez-react.netlify.app/api/articles")
。然后(res=>{
这是我的国家({
文章:res.data.articles,
状态:“成功”
});
});
}
render(){
如果(this.state.articles.length>=1){
var listArticles=this.state.articles.map((article)=>{
返回(
);
});
返回(
{listArticles}
);
}else if(this.state.articles.length==0&&this.state.status==success){
返回(
无关节畸形
托达维娅·海伊·恩斯塔·塞西翁(Todavia NoHay Contendo en esta sección)
);
}否则{
返回(
卡甘多
);
}
}
}
出口违约物品;
研究其他操作系统帖子,但不要提出解决方案
应该注意的是,当我打开页面时,它会像正常加载一样,然后将所有内容留空
我刚刚检查了网络控制台,看看所有的东西都指向了它所在的位置,如果你能看到我的github存储库,看到一些奇怪的东西,我将不胜感激
和my app web这些是由于代码中发生的未经处理的错误而发生的一般性错误,主要是由于预期和接收之间的不匹配 在这种情况下,您需要尝试调试代码,以确定哪些失败或与预期不同。这被认为是使用Javascript的一个缺点,因为它不是类型证明,而且您经常会遇到这样的错误 但是浏览器有助于调试,因此您在上面粘贴的
日志
是chrome的堆栈跟踪
,您可以确定它失败的地方。虽然不能保证,因为有时您可能使用小型脚本,但它非常有用
Top
log,其中说明错误article.js
单击它,它将指向页面中出现此错误的那一行,在那里添加日志,您将得到一个答案
就像看到你可能试图得到一些不可用的东西的长度一样
尝试为res
添加日志,查看收到的内容
axios.get("https://arthuro-gomez-react.netlify.app/api/articles")
.then(res => {
console.log(res)
this.setState({
articles: res.data.articles,
status: 'success'
});
});
}
您的API不起作用,请检查来自insomia的图片 检查您的api。这样你的长度是空的 还是这样试试
Axios.get('http://localhost:yourport/api/someroute')
.then(res =>{
const data = res.data;//to save your data response
console.log("----SETTING DATA-----")
console.log(data)//to see your data response
this.setState({articles}) //to set your data response
console.log("++++++ALL DATA WAS SETTING++++++")
}).catch(err=>{
console.log(err)
})
我已经知道发生了什么,非常尴尬的事情哈哈,我没有选择路线,因为我的后端与netlify不一致,只有我的前端,所以我继续在heroku上传我的后端项目,只连接路线的全局变量,指向指向指向heroku:)的链接,准备好了,解决了,谢谢大家的时间。如果当您得到数据结果时,
res.data.articles
未定义,则会发生这种情况。请检查控制台,查看哪些数据来自API?我忘了提到,在本地主机上,它工作得非常好,这是在生产中出现的错误:CI将尝试console.log以查看res.data.articles显示的内容我显示空数组,但我不明白为什么,如果它在我的联机数据库中,并且是创建的,它显示了我{数据:"我刚和postman核实了一下,它不起作用,它向我显示了一个很长的错误,可能是什么?它是用于生产吗?在localhost中,它非常适合我:检查您的防火墙,如果您使用的是linux服务器,请检查ufw的配置,并允许您尝试访问的端口,检查这篇文章,我也遇到了这个问题。尝试使用这个s命令sudo ufw allow从您的\u ip\u或\u域到您的\u端口的任何端口,例如sudo ufw allow从203.0.113.4到任何端口8080Acabo de intentar con localhost y muestra el articulo con postman好的,我会做的,等等我