Mongoose 无法将新对象推送到父数组

Posted

技术标签:

【中文标题】Mongoose 无法将新对象推送到父数组【英文标题】:Mongoose can't push new obejct to parent array 【发布时间】:2018-07-20 17:05:18 【问题描述】:

我有以下猫鼬模型:

Place.js

const mongoose = require("mongoose")
const Schema = mongoose.Schema

const placeSchema = new Schema(
    title:  type: String, require: true ,
    filename:  type: String, require: true ,
    lociSets: [ type: Schema.Types.ObjectId, ref: 'LociSet']
)

module.exports = mongoose.model("places", placeSchema)

LociSet.js

const mongoose = require("mongoose")
const Schema = mongoose.Schema
const LociSchema = require('./Locus')

const lociSetSchema = new Schema(
    title:  type: String, require: true ,
    creator:  type: Schema.Types.ObjectId, ref: 'User' ,
    public:  type: Boolean, default: true ,
    loci: [LociSchema]
)
module.exports = mongoose.model("lociSets", lociSetSchema)

Locus.js

const mongoose = require("mongoose")
const Schema = mongoose.Schema

const locusSchema = new Schema(
    position: 
        x:  type: Number, require: true ,
        y:  type: Number, require: true ,
        z:  type: Number, require: true 
    
)

module.exports = locusSchema

问题: 我尝试将一个新的 LociSet 插入 Place 的 lociSet 数组中,如下所示:

exports.createOne = async (req, res) => 
    const 
        title,
        public = true,
        loci = []
     = req.body

    console.log(title,public,loci,req.user.id)
    const lociSet = new LociSet(
        title,
        public,
        loci,
        creator: req.user.id
    )

    try 
        const place = await Place.findOne(
            "title": req.params.title.toLowerCase()
        )

        console.log(lociSet)

        await lociSet.save()
        await place.lociSets.push(lociSet)
        await place.save()
     catch (err) 
        res.status(500).send(
            message: "Some error occurred while creating the loci set.", err
        );
    

然后我收到一条错误消息说"Cast to [undefined] failed for value \"[\"title\":\"Test set\",\"creator\":\"5a7898c403999200c4ee3ae5\",\"public\":\"true\"]\" at path \"lociSets\""

创建 LociSet 模型没有问题,但是当我尝试保存位置模型时它似乎中断了

【问题讨论】:

【参考方案1】:

因为lociSetsObjectId 引用的数组,您可能想尝试以下方法:

exports.createOne = async (req, res) => 

    const  title, public = true, loci = []  = req.body
    const lociSet = new LociSet(
        title,
        public,
        loci,
        creator: req.user.id
    )

    try 

        const newLociSet = await lociSet.save()
        const place = await Place.findOneAndUpdate(
             "title": req.params.title.toLowerCase() ,
             "$push":  "lociSets" : newLociSet._id  ,
             "new": true
        )

        res.status(200).json(place)

     catch (err) 
        res.status(500).send(
            message: "Some error occurred while creating the loci set.", err
        )
    

【讨论】:

以上是关于Mongoose 无法将新对象推送到父数组的主要内容,如果未能解决你的问题,请参考以下文章

如何将新值推送到 Mongoose 中的嵌套数组

Mongoose:如何将新值推送到特定键中?

postgresql 函数 - 如何将新的 json 对象推送到 json 数组?

将对象 ID 推送到嵌套数组 MongoDB/Mongoose

VueJS 将新对象推送到数组,数据不是响应式的

使用 Mongoose 将项目推送到对象中的数组