简体   繁体   English

为什么我的控制台没有记录我的代码?

[英]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})
  • Make sure that the API returns a proper answer.确保 API 返回正确的答案。
  • Make sure process.env.REACT_APP_NEWS_ARTICLE_API is returning the correct value as well.确保process.env.REACT_APP_NEWS_ARTICLE_API也返回正确的值。
  • Place a console.log() outside/before the axios promise, make sure the getNews method is being called.在 axios promise 之外/之前放置一个console.log() ,确保正在调用getNews方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我的控制台没有记录搜索输入? - Why is my console not logging search input? 为什么 console.log 从日志记录中排除我的零? - Why does console.log exclude my zero from logging? 为什么我的节点应用程序不登录到终端或Chrome控制台? - Why isn't my node app logging to the terminal or the Chrome console? 为什么我的代码没有记录到控制台? - Why is my code not being logged to the console? 为什么我的Javascript代码只能在控制台中工作? - Why my Javascript code working only in the console? 为什么我的测试失败并且lodash方法中的console.log()语句未记录? - Why are my tests failing and console.log() statements within a lodash method not logging? 当我的 function 似乎返回正确答案时,为什么 console.log() 日志记录未定义? - Why is console.log() logging undefined when my function seems to be returning the right answer? 为什么我的 chrome 扩展按钮仅在扩展弹出窗口中登录控制台而不是用户的网页 - Why is my chrome extension button only console logging in the extension popup and not the user's webpage 为什么我的控制台不显示基于以下代码的结果 - Why doesn't my console show a result based on code below 为什么我的代码不将变量的值打印到控制台? - Why won't my code print the value of a variable to the console?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM