Node.js   发布时间:2022-04-24  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了node.js – 具有引用数组的Mongoose模型模式:CastError:对于值“[object Object]”,Cast to ObjectId失败大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我用express.js和mongoosejs建立了一个博客网站.一篇文章可能有一个或多个类别.当我创建一篇新文章时,我收到错误

{ [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' }

任何人都可以帮助我吗?
相关代码如下:

Article模型定义如下:

var mongoose = require('mongoose'),scheR_884_11845@a = mongoose.scheR_884_11845@a;
var ArticlescheR_884_11845@a = new scheR_884_11845@a({
created: {  type: Date,default: Date.Now},title: String,content: String,sumMary: String,categories: [{ 
    type: scheR_884_11845@a.objectId,ref: 'Category' }]
});
mongoose.model('Article',ArticlescheR_884_11845@a);

而CAtegory模型的定义如下:

var mongoose = require('mongoose'),scheR_884_11845@a = mongoose.scheR_884_11845@a;
var CategoryscheR_884_11845@a = new scheR_884_11845@a({
  parent: {
    type: scheR_884_11845@a.objectId,},name: String,subs: [CategoryscheR_884_11845@a]
});
mongoose.model('Category',CategoryscheR_884_11845@a);

当我创建一篇新文章并将其保存为:

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

调用create函数时,console.log()输出显示如下:

// 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',_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"',path: 'categories' }

我google了很多,但没有运气.请帮我!

更新:
感谢Gergo的回答.
但是,如果我用almoset更新存在的文章相同的代码,它的工作正常!为什么?代码显示如下:

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,categories: [ { _id: 53c934bbf299ab241a6e0524,sort: 0
    } ],created: Sat Jul 19 2014 18:05:40 GMT+0800 (中国标准时间) 
}

这很好用.

解决方法

您的文章架构需要一个ObjectIds数组:

var ArticlescheR_884_11845@a = new scheR_884_11845@a({
  ...
  categories: [{ 
    type: scheR_884_11845@a.objectId,ref: 'Category' }]
});

但是req.body包含一个类别对象:

categories:
   [ { _id: '53c934bbf299ab241a6e0524',sort: 1 } ]

并且Mongoose无法将类别对象转换为ObjectId.这就是你得到错误的原因.确保req.body中的类别仅包含Id

{ title: 'This is title',categories: [ '53c934bbf299ab241a6e0524' ],updated: [ 1405697477413 ] }

大佬总结

以上是大佬教程为你收集整理的node.js – 具有引用数组的Mongoose模型模式:CastError:对于值“[object Object]”,Cast to ObjectId失败全部内容,希望文章能够帮你解决node.js – 具有引用数组的Mongoose模型模式:CastError:对于值“[object Object]”,Cast to ObjectId失败所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。