turbagu
turbagu

Reputation: 13

Sending an object with a post request (JS)

So I'm creating an object named "ob" in my post request function, and it prints the right thing to the console when I use console.log, but when I try to send that object the returned value does not resolve to an object. Is using "res.status(200).send(ob)" wrong here?

app.post('/', (req, res) => {

        var ob = {
            id: Math.random().toString(),
            username: req.body.username
        }

        console.log("object is");
        console.log(ob);

        res.status(200).send(JSON.stringify(ob));  
}

I tried returning both JSON.stringify(ob) as well as ob but they both don't return an object

Upvotes: 0

Views: 767

Answers (1)

RaqDeku
RaqDeku

Reputation: 27

Use res.status(200).json(ob) instead.That would send the ob as an object rather than JSON.stringify(ob) which converts a JSON into a string.

Upvotes: 1

Related Questions