Mongoose - 在path \“responsavel \”

时间:2015-05-14 15:21:29

标签: node.js mongodb mongoose restangular

我是MEAN堆栈的新手,过去3天一直在研究它。

当我尝试使用one-to-may关系保存新对象时出现此错误。我使用的是restangular + mongoose,无法找到错误的来源。

这是表单提交时我正在执行的保存:

$scope.saveProjeto = function() {
  console.log($scope.projeto);
  Projeto.post($scope.projeto).then(function() {
    $location.path('/projetos');
  });
};

这是我的用户模型:

var mongoose = require('mongoose')
Schema = mongoose.Schema,
relationship = require('mongoose-relationship');

// Creating Schema
var UserSchema = Schema({
  nome: {
    type: String,
    required: true
  },
  sobrenome: {
    type: String,
    required: false
  },
  email: {
    type: String,
    required: true
  },
  password: {
    type: String,
    required: true
  },
  projetos: [{
    type: Schema.ObjectId, ref: "Projeto"
  }]
});

// Exporting Model Schema
module.exports = UserSchema;

这是我的Projeto模型:

var mongoose = require('mongoose'),
Schema = mongoose.Schema,
relationship = require('mongoose-relationship');

// Creating Schema
var ProjetoSchema = Schema({
  nome: {
    type: String,
    required: true
  },
  responsavel: {
    type: Schema.ObjectId,
    ref: "User",
    required: true
  },
  descricao: {
    type: String
  },
  data_inicio: {
    type: Date,
    required: true
  },
  data_fim_prevista: {
    type: Date,
  },
  data_fim: {
    type: Date,
  },
  created_at: {
    type: Date,
    required: true
  },
  updated_at: {
    type: Date,
    required: true
  }
});

这就是我在控制台上得到的结果:

message: "Cast to ObjectId failed for value "[object Object]" at path 

"responsavel""
name: "CastError"
path: "responsavel"
type: "ObjectId"
value: {_id: "5553afb5d65550216c26d69f", nome: "Renato", sobrenome: "Correa", email: "renatojf2@gmail.com",…}
__v: 0
_id: "5553afb5d65550216c26d69f"
email: "foobar@gmail.com"
fromServer: true
nome: "Renato"
parentResource: null
password: "foobar"
projetos: []
reqParams: null
restangularCollection: false
restangularized: true
route: "users"
sobrenome: ""

0 个答案:

没有答案