1

Question is simple, but not answered perfectly yet for me (perhaps only for me but is not totaly clear..)

Question : I want to return MongoDB from "collection.findOne" with mongo.. is ok AND JSON.stringify() this informations for send to another service...

// i past a pseudo code for response :-)
collection.find({id_to_find: id_to_find}, function(err, results) {
 if (err){
    console.log ("error find");
 }if (results) { // update for good syntax !
    var results = JSON.stringify(results); // error, why ??? <if not this line, is ok, but i want stringify !>
    res.json({
        returnJSON: results
    });
 }
}
////////////////////////////////////
// example of a mongo object return :
 [ { _id: 1,
    property: 'xxxx',
    etc: 'xx'
   },
  { _id: 2
    property: 'xxxx',
    etc: 'xxxx'
  }
 ]

Next time, i have severals records like results_mongo = [{mongo object datas},{ etc.. }] // like an array

i want with my server node.js to JSON.stringify my collection && return theses results..

The error is ::::: 

    TypeError: Converting circular structure to JSON
    at Object.stringify (native)

Response ?

(thank to correct object to result, my question is how to exept the stringify deep object.. :)

for searchs, a goods posts on stack : Convert Mongoose docs to json

How do you turn a Mongoose document into a plain object?

obysky
  • 761
  • 5
  • 8

1 Answers1

0
// you are passing object to your stringify method.
var object = JSON.stringify(object); 

// you need to pass results as your callack method returns results
var object = JSON.stringify(results); 
Nielarshi
  • 1,126
  • 7
  • 11
  • The error is TypeError: Converting circular structure to JSON at Object.stringify (native) – obysky Mar 07 '15 at 19:15
  • In Node.js, you can use util.inspect(results), It should replace all circular links with "[Circular]". – Nielarshi Mar 07 '15 at 19:27
  • no way to do that !!! - I've 2 or 3 deep objects... like var obj = {a:{b:{c:{ggg}}}} – obysky Mar 07 '15 at 19:29
  • deep object is fine but what is the need of having some object as value which again has this object as value of any of its property. Instead in your database, you can have references stored of other documents. It seems your schema is not proper. – Nielarshi Mar 07 '15 at 19:36
  • my mongoShemas is propers, in not JSON.stringify situation is ok.. and.. i have a userShema, have a simple location field. This field is containt String (for mongo) of Objects insides.. simple navigator.geolocation results... [[[[ is just example of my datas :-) ]]] – obysky Mar 07 '15 at 20:09