繁体   English   中英

访问JavaScript'[object Object]'

[英]Accessing JavaScript '[object Object]'

我有一个在node / express上运行的应用程序。 我用[object Object]绊倒了自己。

我这样渲染页面

app.get('/members', checkAuthentication, function(req, res){
  res.render('members', { user: req.user });
});

要访问我页面上的用户电子邮件(.ejs),我可以这样做

<%= user.email %>

我有一些客户端逻辑,然后在这里我想将用户的电子邮件地址传递给服务器以进行特定的处理。 再次很简单

body: JSON.stringify({user_email: "<%= user.email %>", token: token})

服务器端

app.post("/charge", (req, res) => {
  console.log(req.body.user_email)
  //'myEmailAddress@domain.com'
}

我绊倒自己的部分似乎是如果我想传递整个用户对象

body: JSON.stringify({user: "<%= user %>", token: token})

服务器端

app.post("/charge", (req, res) => {
  console.log(req.body.user)
  //'[object Object]'
}

例如,我现在如何访问用户的电子邮件?

谢谢

我不确定您是否使用body-parser 将此包添加到app.js (假设)

const bodyParser = require('body-parser');

在路线添加此行之前,

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));

编辑:

app.post("/charge", (req, res) => {
  console.log(JSON.parse(req.body.user))
}

如果您想查看req.body.user包含/看起来像什么,请尝试:

console.log(JSON.stringify(req.body.user))

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM