使用Mongoose定义的Schema包含数组,保存的时候出错CastError: Cast to ObjectId failed for value “[object Object]”


我使用Express.js和mongoose创建一个类似博客系统,一篇文章有若干个类别。

文章的Schema定义如下:


 var mongoose = require('mongoose'),
Schema = mongoose.Schema;
var ArticleSchema = new Schema({
created: {  type: Date, default: Date.now},
title: String,
content: String,
summary: String,
categories: [{ 
  type: Schema.ObjectId, 
  ref: 'Category' }]
});
mongoose.model('Article', ArticleSchema);

类别的Schema定义如下:


 var mongoose = require('mongoose'),
Schema = mongoose.Schema;
var CategorySchema = new Schema({
  parent: {
    type: Schema.ObjectId,
  },
  name: String,
  subs: [CategorySchema]
});
mongoose.model('Category', CategorySchema);

保存文章的代码如下:


 exports.create = function(req, res) {
  console.log(req.body);
  var article = new Article(req.body);
  article.user = req.user;
  console.log(article);
  article.save(function(err) {
    if (err) {
        console.log(err);
        return res.jsonp(500, {
          error: 'Cannot save the article'
      });
    }
    res.jsonp(article);
  });
};

保存的时候,这里的输出如下:


 // console.log(req.body);
{ title: 'This is title',
  content: '<p>content here</p>',
  categories:
   [ { _id: '53c934bbf299ab241a6e0524',
     name: '1111',
     parent: '53c934b5f299ab241a6e0523',
     __v: 0,
     subs: [],
     sort: 1 } ],
  updated: [ 1405697477413 ] }

// console.log(article);
{ 
  title: 'This is title',
  content: '<p>content here</p>',
  _id: 53c93dc5b1c3b8e80cb4936b,
  categories: [],
  created: Fri Jul 18 2014 23:31:17 GMT+0800 (中国标准时间) }

// console.log(err);
{ [CastError: Cast to ObjectId failed for value "[object Object]" at path "categories"]
  message: 'Cast to ObjectId failed for value "[object Object]" at path "categories"',
  name: 'CastError',
  type: 'ObjectId',
  value: [ [object Object] ],
  path: 'categories' }

出错就是在Category这个数组的地方。请问这个怎么解决呢?我已经搜索了很多,但是都没有结果,请大神帮忙。

更新
但是我使用几乎相同的代码更新一个已经存在的文章,就不会出现问题,应该不是类型的问题,代码如下:


 var mongoose = require('mongoose'),
    Category = mongoose.model('Category'),
    _ = require('lodash');

exports.update = function(req, res) {
console.log(req.body);
var article = req.article;
article = _.extend(article, req.body);
console.log(article);
article.save(function(err) {
    if (err) {
        return res.jsonp(500, {
            error: 'Cannot update the article'
        });
    }
    res.jsonp(article);

  });
};

这段代码的输出如下:


 // console.log(req.body);
{ _id: '53ca42f418bfb23c1e04df02',
    summary: 'tttt',
    title: 'tt',
    content: '<p>tttt</p>',
    __v: 2,
    categories: [ { _id: '53c934bbf299ab241a6e0524', name: '1111' } ],
    created: '2014-07-19T10:05:40.183Z'
}

// console.log(article);
{ _id: 53ca42f418bfb23c1e04df02,
    title: 'tt',
    content: '<p>tttt</p>',
    __v: 2,
    categories: [ { _id: 53c934bbf299ab241a6e0524, name: '1111', subs: [], sort: 0
    } ],
    created: Sat Jul 19 2014 18:05:40 GMT+0800 (中国标准时间) 
}

这样就能正常工作,这是为什么呢?

express.js node.js mongodb mongoose

朴实无华的面包 9 years, 9 months ago

你在保存的时候 categories 里面应该放 Category ObjectId ,而不是完整的内容。


 {
  title: "This is title",
  content: "<p>content here</p>",
  categories: [new ObjectID("53c934bbf299ab241a6e0524")] // 这里应该是 oid 实例,而不是整个对象
}

题主后面那个执行正常的例子应该是因为 req.article.categories 里存的已经是 Document 对象了,所以恰好跳过了 oid( ObjectID )检查。

详见 mongoose 3.8.13 源码 lib/schema/objectid.js 81 ~ 91 行。


 if (value instanceof Document) {
  value.$__.wasPopulated = true;
  return value;
}

// setting a populated path
if (value instanceof oid) {
  return value;
} else if (Buffer.isBuffer(value) || !utils.isObject(value)) {
  throw new CastError('ObjectId', value, this.path);                        
}

MEKY君 answered 9 years, 9 months ago

Your Answer