首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >“快速”和“猫鼬”属性未保存。

“快速”和“猫鼬”属性未保存。
EN

Stack Overflow用户
提问于 2018-03-07 09:46:48
回答 1查看 37关注 0票数 0

TL;DR:我试图保存一个新的对象,其中一个字段没有保存,其他字段保存得很好。

我有一个带有名为superPlotId的属性的猫鼬模式:

代码语言:javascript
运行
复制
const mongoose = require('mongoose');
const GeoJSON = require('mongoose-geojson-schema');
const Schema = mongoose.Schema;
const plotSchema = new Schema(
    {
        ...fields...
        superPlotId: String,
        ...more fields
    },
    { strict: false },
    { bufferCommands: false }
);

//create model class
const ModelClass = mongoose.model('plot', plotSchema);

//export model
module.exports = ModelClass;

我正在尝试保存一个与Express匹配此模式的新对象,如下所示:

代码语言:javascript
运行
复制
exports.newPlot = async (req, res, next) => {
    const {
        ...a bunch of fields...
        superPlotId
    } = req.body.props;
    const plot = new Plot({
        ...a bunch of fields...
        superPlotId
    });
    console.log(('new plot:', JSON.stringify(plot)));
    try {
        const newPlot = await plot.save();
        res.json(newPlot);
    } catch (e) {
        console.log("couldn't save new plot", JSON.stringify(e));
        return res.status(422).send({ error: { message: e, resend: true } });
    }
};

我知道一个格式正确的对象正在命中端点,因为上面的console.log显示了它:

代码语言:javascript
运行
复制
{...bunch of fields..."superPlotId":"5a9e9f9f0f8a8026005fe1e7"}

然而,在没有superPlotId字段的情况下,这个图出现在我的数据库中。

有人知道我在这里错过了什么吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-03-07 09:54:28

尝尝这个

代码语言:javascript
运行
复制
try {
    let plot = new Plot();
    plot = Object.assign(plot, req.body.props);
    const newPlot = await plot.save();
    res.json(newPlot);
} catch (e) {
    console.log("couldn't save new plot", JSON.stringify(e));
    return res.status(422).send({
        error: {
            message: e,
            resend: true
        }
    });
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49148720

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档