0

I am using node, angular and mongodb. I am trying to add an array of objects (coming from angular client) to the db using node. The problem is that the server keeps producing '[object Object]' instead of the actual array.

Code:

// Schema
var testSchema = new mongoose.Schema({
    name: String,
    videos: [ {
        vid:    String, // id of the video
        start:  String, // desired start time
        end:    String,  // desired end time
        type:   String
    }]
});

// Model
var Test = mongoose.model('Test', testSchema);

// Add new test
app.post('/api/tests', function (req, res, next) 
{
    // videos from client, output suggests that this is fine    
    console.log( req.body.testVideos );

    // now creating it and testVideos not fine anymore
    var test = new Test({
        name: req.body.testName,
        videos: req.body.testVideos
    });

    // see output
    console.log( test );

    test.save(function(err)
    {
        if (err) return next(err);
        res.send(200);
    });
});

Output:

[ { vid: 'vid', start: 'start', end: 'end', type: 'type' } ]

{ name: 'name',
  videos: [ '[object Object]' ] } // this is the problem

What do I need to do to get around this problem?

Ben
  • 15,938
  • 19
  • 92
  • 138
  • You can parse the json and retrieve .videos.. – Subburaj Oct 16 '14 at 13:23
  • I solved the problem. Apparently, one cannot use 'type' as an attribute, or otherwise this problem appears, see Pierre's answer here: http://stackoverflow.com/questions/19695058/how-to-define-object-in-array-in-mongoose-schema-correctly-with-2d-geo-index – Ben Oct 16 '14 at 13:32
  • 2
    I did this to myself yesterday by accident even though I knew Mongoose used `type` to define... well, the type of a property. It still took me 5-10 minutes to figure out and I already *knew* it :/ You should add an answer directly to this question, though, to avoid it being buried. – Dave Newton Oct 16 '14 at 13:42

1 Answers1

0

You should check out util.inspect(). It's designed specifically for this.

Example:

var util = require('util');

// your codez...

console.log(util.inspect(test));
jmar777
  • 38,796
  • 11
  • 66
  • 64