[英]Why is my console is not logging my code?
Can you help with this issue - I am unable to see a log in the console.你能帮忙解决这个问题吗?我无法在控制台中看到日志。 I was trying to log/view the data in the console with the codes below, but when i run it nothing shows.
我试图用下面的代码在控制台中记录/查看数据,但是当我运行它时没有显示。 I think I have it correctly, but I might be wrong.
我想我说得对,但我可能错了。 Can you help?
你能帮我吗?
import React, { Component } from 'react' import { Container, Row, Col } from 'bootstrap-4-react'; import News from '../Articles/News'; import Post from '../Posts/Post/Post'; import axios from 'axios'; export default class Body extends Component { state = { title: "", description: "", urlToImage: "", newsPost: [] } componentDidMount = () => { this.getNews(); } getNews = async() => { const news = await axios.get(`https://newsapi.org/v2/top-headlines?sources=techcrunch&apiKey=${process.env.REACT_APP_NEWS_ARTICLE_API}`).then((res) => { const data = res.data; this.setState = ({newsPost: data}); console.log("newsPost Recieved"); //cannot see this in the console. }) } render() { return ( <Container id="bodycontainer" className="container"> <Row className="rowstyles"> <Col className="newscolumn" col="12 sm-6 md-8"> <Post /> </Col> <Col className="contentcolumn" col="6 md-3"> <div className="centersources">Sources</div> </Col> </Row> </Container> ) } }
There's a lot of possibilities here since you're not providing the full code and we can't even debug this.这里有很多可能性,因为您没有提供完整的代码,我们甚至无法调试它。 So I'll just mention a couple of suggestions:
所以我只提几个建议:
this.setState = ({newsPost: data})
should be this.setState({newsPost: data})
. this.setState = ({newsPost: data})
应该是this.setState({newsPost: data})
。process.env.REACT_APP_NEWS_ARTICLE_API
is returning the correct value as well.process.env.REACT_APP_NEWS_ARTICLE_API
也返回正确的值。console.log()
outside/before the axios promise, make sure the getNews
method is being called.console.log()
,确保正在调用getNews
方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.