JavaScript Answers

How to convert Mongoose docs to JSON with Node.js?

Spread the love

To convert Mongoose docs to JSON with Node.js, we use the lean method.

For instance, we write

  .exec((err, users) => {
    return res.end(JSON.stringify(users));

to call lean to convert the results returned from find to a plain object.

Then we get the results from the users parameter in the exec callback.

users is a plain JavaScript array.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *