将子文档推送到父数组时遇到问题
Having trouble pushing sub document to parent array
我有一个用户模型,其中包括一个位置模式:
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));
};
当我试图向我得到的位置发送curl POST请求时:
{"error":{"message":"this._schema.caster.cast is not a function","error":{}}}
console.log用户、用户位置和位置user.locations.push(location);行返回的正是我所期望的。我很确定这个错误源于push函数调用。任何见解都将不胜感激。
您的嵌入位置模型
const locationSchema = require('./location.js');
所以只有你得到这个错误,模型不能嵌入模式只能嵌入
const locationSchema = require('./location.js'). schema;
所以你试试这个
相关文章:
- 将子文档推送到父数组时遇到问题
- AngularJs 帮助,在传递给隔离范围的对象中选择数组时遇到问题
- 解析数组时遇到困难
- 在 javascript 数组中播放音频文件时遇到问题
- 显示 JavaScript 数组时遇到问题
- 比较数组时遇到麻烦
- 返回数组元素时遇到问题
- 我在使用数组及其元素时遇到问题
- 获取 get JSON 显示数组中的值时遇到问题
- 在设置条件以仅在数组中存在相应对象时才执行函数时遇到问题
- 尝试将IF语句与数组一起使用时遇到问题
- 在for循环数组检查和JSON变量方面遇到问题
- 从数组中删除对象时遇到麻烦
- 在JavaScript/jQuery中传递整个二维数组作为参数时遇到麻烦
- 在angular(循环中的循环)中创建新的对象数组时遇到麻烦
- 在javascript中访问数组值时遇到麻烦
- 在Angular控制器中解析json/string版本的数组时遇到问题
- 我在添加数组的这些元素时遇到了麻烦.破折号似乎抑制了每个变量的添加
- 在将特定键推入数组时遇到困难
- 将数组中的值传递到另一页时遇到麻烦