出来的Axios数据是不确定的 [英] Axios data coming out to be undefined

查看:86
本文介绍了出来的Axios数据是不确定的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在nodeJs中有一条路由,看起来像这样

I have a route in nodeJs which looks like this

app.get("/", (req, res) => {
  console.log(req.user)
 res.json(req.user)
})

此处console.log显示未定义,但如果我访问地址(localhost:3000/)

Here console.log showing undefined but if I visit the address (localhost:3000/)

它显示以下数据(在json中)

it displays the following the data (in json)

// 20181025193337
// http://localhost:3000/

{
  "isFormFilled": false,
  "_id": "5bd1a3d82101d36407f81218",
  "username": "Rohit Bhatia",
  "userId": "102775073203963169965",
  "image": "https://lh5.googleusercontent.com/-7HxFRQOCd9Q/AAAAAAAAAAI/AAAAAAAAAU8/pgzBQd9X6pA/photo.jpg?sz=250",
  "__v": 0
}

类似地,从我的react应用程序中,我发出axios请求,其中response.data出来的是不确定的(或")

Similarly from my react app, I making a axios request where response.data is coming out to be undefined (or "")

 componentWillMount() {
         axios.get("http://localhost:3000/").then(response => {
             console.log(response)
         }).catch(error => {
             console.log(error)
         })
    }

如果我在自己的路线上做类似的事情

If I do something like this in my route

 app.get("/", (req, res) => {
      console.log(req.user)
     res.json("here)
    })

我的axios请求中的数据显示为"Here"

the data in my axios request is showing as "Here"

[问题:] 为什么会发生这种情况,或者我在这里可能做错了什么?

[Question:] Why could this be happening or what could I be doing wrong here?

Ps :自从我在本地发出请求以来,我已启用了crocs资源共享

Ps: I have enabled crocs resource sharing since I am making request locally

推荐答案

您正在console.logging req.user,它不在您的对象中,因此为什么它是未定义的. console.log(req.userId)console.log(req.username)并注意已定义

You're console.logging req.user which isn't in your object, hence why it's undefined. console.log(req.userId) or console.log(req.username) and notice that it is defined

这篇关于出来的Axios数据是不确定的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆