3

I have tried these questions Converting an object to string and How to convert req.body to string, but unfortunately they didn't solve my problem, I'm posting data using postman and I want to add a string at specific location in received data. but when I tried to do so I either get [object Object] or {} instead of my original body content Here is my code

var express = require("express"),
    bodyParser = require('body-parser');
var app = express();
app.use(bodyParser.text({ type: '*' }));

app.post('/users', function (req, res, body) {
    var newData = `
    func addArtist(){
    //Anything
}
`;
    var body = req.body
    var abc = body.toString();
    var strlen = abc.length;
    var strlen = (strlen - 1);
    var txt2 = abc.slice(0, strlen) + newData + abc.slice(strlen);
    res.send(txt2);
});

app.listen(3000);

req.body is

func anyName(){
    //Body of Function
}

Output is:

[object Object
    func addArtist(){
    //Anything
}]

Expected Output is:

func anyName(){
        //Body of Function
        func addArtist(){
        //Anything
    }
}

1 Answers1

2

Try this JSON.stringify(req.body);

Then this to convert back JSON.parse(req.body)

caden311
  • 972
  • 8
  • 22