Error Converting circular structure to JSON

0

When listing the elements in the database I get the error "Converting circular structure to JSON", how can I solve it?

BD Model

const mongoose = require('mongoose');
const {Schema} = mongoose;

const PostSchema = new Schema({
    text: { type: String, required: true},
    created_at: Date,
});

module.exports = mongoose.model('Post', PostSchema);

List the data

 import express from 'express';
 const router = express.Router();
 const Post = require('../model/post');


  //todos los post
 router.get('/posts', function (_req, res) {
        const mini = Post.find();
        res.json(mini);
    });
 module.exports = router;
    
asked by daniel alfaro 15.11.2018 в 02:47
source

1 answer

1

Try it like this:

router.get('/posts', function (_req, res) {
    Post.find()
        .exec(function(err, posts) {
            res.json(posts); 
        });
});

The find function returns a Query type object, does not return the data directly, you have to call exec on this object and pass it a callback . The error is because you can not serialize the object as JSON.

    
answered by 15.11.2018 / 03:04
source