0
我正在使用node,angular和mongodb。我试图添加一个对象数组(来自角度客户端)使用节点的数据库。问题在于服务器不停地生成'[object Object]'而不是实际的数组。'[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 .. – Subburaj 2014-10-16 13:23:46
我解决了这个问题。显然,不能使用'type'作为属性,否则会出现此问题,请参阅Pierre的答案:http://stackoverflow.com/questions/19695058/how-to-define-object-in-array-in-mongoose -schema-correct-with-2d-geo-index – Ben 2014-10-16 13:32:50
昨天我偶然发现了这个,尽管我知道Mongoose使用'type'来定义...以及属性的类型。它仍然花了我5-10分钟才弄清楚,而且我已经知道了它:/但你应该直接为这个问题添加一个答案,以避免它被掩埋。 – 2014-10-16 13:42:18