mobutt mobutt - 1 year ago 87
Javascript Question

How to convert req.body to string?

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.json());'/addUser', function (req, res) {

Output is:

[object Object]

When using JSON.stringify the output is:

" [object Object] "

Answer Source

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