0
我有一個用戶模型,其中包括它locationsSchema:遇到問題推子文件到父陣列
const locationSchema = require('./location.js');
const userSchema = new mongoose.Schema({
email: {
type: String,
unique: true,
required: true,
},
token: {
type: String,
require: true,
},
locations: [locationSchema],
passwordDigest: String,
}, {
timestamps: true,
});
我的位置模式是:
const mongoose = require('mongoose');
const locationSchema = new mongoose.Schema({
name: {
type: String,
required: true
},
city: {
type: String,
},
region: {
type: String,
},
country: {
type: String,
},
coords: {
lat: {
type: Number,
require: true
},
long: {
type: Number,
require: true
},
},
visited: {
type: Boolean,
require: true,
default: false,
},
comment: {
type: String,
},
}, {
timestamps: true,
});
終於在我的控制器創建操作對於位置是:
const controller = require('lib/wiring/controller');
const models = require('app/models');
const User = models.user;
const Location = models.location;
const create = (req, res, next) => {
User.findById(req.body.user.id).then (function(user){
let location = Object.assign(req.body.location);
user.locations.push(location);
return user.save();
})
.then(user => res.json({ user }))
.catch(err => next(err));
};
當我試圖發送一個捲曲POST請求的位置,我得到:
{"error":{"message":"this._schema.caster.cast is not a function","error":{}}}
console.logging用戶,user.locations和位置僅僅是 user.locations.push(位置)之前; 行返回正是我所期望的。我相當確定這個錯誤是由推送函數調用產生的。任何有識之士將不勝感激。