2

I got an error while console mongoose object. Here is my code:-

var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/test');
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function () {
    console.log('Yea! we are connected.');
});
console.log(JSON.stringify(mongoose));

Here is Error Message:-

enter image description here

3
  • 1
    The error is pretty obvious. Why do you want to convert the mongoose object to JSON anyways? Commented Jan 7, 2016 at 7:49
  • What are you trying to do? That's wrong you are trying to stringify the module object. Commented Jan 7, 2016 at 7:50
  • I just want to see what data are exits in mongoose object. If i use console.log(mongoose) i got result but not appropriates.@qqilihq,@Michelem Commented Jan 7, 2016 at 8:04

2 Answers 2

2

You may want to try the util.inspect(object) method which returns a string representation of an object:

var util = require('util');
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/test');
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function () {
    console.log('Yea! we are connected.');
});
console.log(util.inspect(mongoose, { showHidden: true, depth: null }));
Sign up to request clarification or add additional context in comments.

1 Comment

I can see whole mongoose attributes and members with naked eye.Thanks @chridam.
2

inside mongoose module have this logic

Mongoose.prototype.Mongoose = Mongoose;

It makes Circular structure ( object self-reference) . You cant default stringify an Circular Object. If you want to use JSON.stringify add your custom logic handle

JSON.stringify(obj,function(k,v){ //logic})

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.