本文介绍了如何将req.body转换为字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将req.body保存为节点中的字符串,但是每当我执行console.log(req.body.toString)时,输出均为[object Object].对我可能做错的事有任何想法吗?

I'm attempting to save req.body to a string in node however whenever I do console.log(req.body.toString) the output is [object Object]. Any idea on what i could be doing wrong?

var express = require('express');
var app = express();
var fs = require("fs");
var bodyParser = require("body-parser");
app.use(bodyParser.urlencoded({extended:false}));
app.use(bodyParser.json());

app.post('/addUser', function (req, res) {
    console.log(req.body.toString());
    res.end("thanks\n");
})

输出为:

[object Object]

使用JSON.stringify时,输出为:

When using JSON.stringify the output is:

" [object Object] "

推荐答案

使用JSON.stringify()将任何JSON或js Object(非圆形)转换为字符串.因此,在您的情况下,以下方法将起作用.

Use JSON.stringify() to convert any JSON or js Object(non-circular) to string.So in your case the following will work.

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

这篇关于如何将req.body转换为字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 23:05