【问题标题】:Mongoose can't push new obejct to parent arrayMongoose 无法将新对象推送到父数组
【发布时间】: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 模型没有问题,但是当我尝试保存位置模型时它似乎中断了

【问题讨论】:

    标签: node.js mongodb express asynchronous mongoose


    【解决方案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
            })
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2019-04-08
      • 1970-01-01
      • 2015-11-07
      • 1970-01-01
      • 1970-01-01
      • 2019-03-28
      • 1970-01-01
      • 2020-10-11
      • 2020-04-29
      相关资源
      最近更新 更多