2016-04-06 4 views
1

değeri için başarısız oldu. Gönderilen verileri aldığımda ve ayrıştırmayı denediğimde bir hata oluştu. İki model varCastError: Array için Cast,

:

var tags = require('../tag/tag.model'), 
    mongoose = require('mongoose'), 
    Schema = mongoose.Schema; 

var OpportunitiesSchema = new Schema({ 
    employeeTitle: String, 
    tagline: Boolean, 
    description:String, 
    location:String, 
    isPreferred:Boolean, 
    tags: [ 
     { 
     type:Schema.Types.ObjectId, 
     ref:'Tag' 
     } 
    ] 
}); 

module.exports.opportunities = mongoose.model('Opportunities', OpportunitiesSchema); 

Ve ben başka bir model vardır: böyle

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

var TagSchema = new Schema({ 
    id: Number, 
    name: String 
}); 

module.exports = mongoose.model('Tag', TagSchema); 

Benim işlevi:

var Opportunities = require('./opportunities.model').opportunities; 
exports.create = function(req, res) { 
    //addOpportunitites.save(req.body, function(err, tag) { 
    // if(err) { return handleError(res, err); } 
    // return res.json(201, tag); 
    //}); 

    var newOpportunity = new Opportunities(req.body); 

    newOpportunity.save(function(err) { 
    if(err) { 
     console.log(err); 
    } else { 
     return res.json(201); 
    } 

    }) 
}; 

Bu

soru hakkında detay

Böyle bir veri gönderdiğinde:

{ 
    "employeeTitle": "Digital Agency Employee, downtown Chicago", 
    "tagline": "My company is offering $1000 referral bonus and I'll give you half!", 
    "description": "<p>Looking for someone who has the following abilities: <ul><li></li></ul></p>", 
    "location": "", 
    "tags": [ 
    545454,45467 
    ] 
} 

Orada bir hatadır:, etiket modelinde tahminen id, Numbers yolluyoruz tags yılında

{ 
    [ValidationError: Opportunities validation failed] 
    message: 'Opportunities validation failed', 
    name: 'ValidationError', 
    errors: { 
     tags: { 
      [CastError: Cast to Array failed for value "545454,45467" at path "tags"] 
      message: 'Cast to Array failed for value "545454,45467" at path "tags"', 
      name: 'CastError', 
      kind: 'Array', 
      value: [Object], 
      path: 'tags', 
      reason: undefined 
     } 
    } 
} 

cevap

2

ancak Opportunities modelinde , mongo tarafından üretilen _id olan ObjectId s depolıyorsunuz.

+0

Kodda nasıl değişiklik yapmam gerektiğini söyler misiniz? – fhlkm

+0

Kodunuzun geri kalanının neye benzediğine ve ileri ve geri iletmek istediğiniz veri türlerine göre değişir. POST verilerinin 'tag'lerini ObjectID olarak bir string olarak değiştirirseniz (örn." 561fa3aac09d1fa4eb63f806 "'), olduğu gibi çalışmasını beklerim. Alternatif olarak, onları mevcut 'id' alanınızı kullanarak arka uçtaki kontrolörde görebilirsiniz. – swider