繁体   English   中英

尝试在 Nodejs 上使用 mongoose 在 MongoDB 中保存嵌套文档

[英]Trying to save nested document in MongoDB using mongoose on Nodejs

我试图将地址另存为以下格式的嵌套文档

在客户端,我以以下格式发送数据:

const address = JSON.stringify( { addressline1:form.addressline1.value, addressline2:form.addressline2.value, city: form.city.value, state: form.state.value, pincode:form.pincode.value } );

const res = await fetch('/candidate/contact-details',{
                method: 'POST',
                body: JSON.stringify( { address } ), 
                headers: {
                    'CSRF-Token': csrfToken,
                    'Content-Type': 'application/json'
                }
            });

在服务器端:

module.exports.contactdetail_post = async (req,res) => {

    const token = req.cookies.jwtauthtoken;
    const { address} = req.body;

    try{
        const contactdetail = await ContactDetail.create({ address});
        await PersonalDetail.findOneAndUpdate(
            { candidate: await getUserId(token)}, // find a document with that filter
            {address}, // document to insert when nothing was found
            {upsert: true, new: true, runValidators: true}, // options
            function (err, doc) { // callback
                if (err) {

    console.log({address});
                    const errors = handleErrors(err);
                } else {
                    res.status(200).json({success: 'saved'});
                }
            }
        );
        res.status(200).json({ success: 'Success' });
    }
    catch(err){
        console.log(err);
        const errors = handleErrors(err);
        res.status(406).json({errors});
    }




}

MySchema 如下所示: 这是联系人详细信息的架构。

const contactdetailSchema = new mongoose.Schema({
    address: {
        addressline1: {
            type: String,
            required: [true, 'Please enter your address'],
            minlength:200
        },
        addressline2: {
            type: String,
            minlength:200
        },
        city: {
            type: String,
            required: [true, 'Please selet your city']
        },
        state: {
            type: String,
            required: [true, 'Please select your state']
        },
        pincode: {
            type: Number,
            required: [true, 'Please enter your Pincode'],
            minlength:6
        }
    },
    candidate: {
        type: mongoose.Schema.Types.ObjectId,
        ref: 'User'
    }
});

我的控制台输出:

在 processTicksAndRejections (internal/process/task_queues.js:79:11) { 属性:[对象],种类:'必需',路径:'address.addressline1',值:未定义,原因:未定义,[Symbol(mongoose:validatorError) )]: true } }, _message: '验证失败' }

此错误显示所有参数(addressline2、city、state、pincode)

 await PersonalDetail.findOneAndUpdate(
            { candidate: await getUserId(token)}, // find a document with that filter
            {address: JSON.parse(address) }, // This line is changed 
            {upsert: true, new: true, runValidators: true}, // options
            function (err, doc) { // callback
                if (err) {

    console.log({address});
                    const errors = handleErrors(err);
                } else {
                    res.status(200).json({success: 'saved'});
                }
            }
        );

像这样更改我的代码工作正常。 其他代码保持不变

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM