【问题标题】:'[object Object]' instead of actual array'[object Object]' 而不是实际的数组
【发布时间】:2014-12-11 21:19:24
【问题描述】:

我正在使用节点、角度和 mongodb。我正在尝试使用节点将一组对象(来自角度客户端)添加到数据库中。问题是服务器一直在生成 '[object Object]' 而不是实际的数组。

代码:

// 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);
    });
});

输出:

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

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

我需要做些什么来解决这个问题?

【问题讨论】:

  • 您可以解析json并检索.videos..
  • 我解决了这个问题。显然,不能使用“类型”作为属性,否则会出现此问题,请参阅 Pierre 的回答:stackoverflow.com/questions/19695058/…
  • 我昨天无意中对自己做了这个,尽管我知道 Mongoose 使用type 来定义......嗯,属性的类型。我仍然花了 5 到 10 分钟才弄清楚,而且我已经知道了:/ 不过,您应该直接在这个问题上添加一个答案,以免它被埋没。

标签: node.js mongodb mongoose


【解决方案1】:

您应该查看util.inspect()。它是专门为此设计的。

例子:

var util = require('util');

// your codez...

console.log(util.inspect(test));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-05-14
    • 1970-01-01
    • 2020-08-11
    • 1970-01-01
    • 2012-09-21
    • 1970-01-01
    • 2021-01-01
    • 2022-10-21
    相关资源
    最近更新 更多