Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/415.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/meteor/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Node.js:使用Mongoose更新文档_Javascript_Node.js_Mongodb_Mongoose - Fatal编程技术网

Javascript Node.js:使用Mongoose更新文档

Javascript Node.js:使用Mongoose更新文档,javascript,node.js,mongodb,mongoose,Javascript,Node.js,Mongodb,Mongoose,我正在尝试在受邀请的用户注册时更新邀请。邀请有一个auth属性,它是一个嵌套对象,它本身有一个键为“used”的属性。我只是尝试使用async/await显式声明该值为true并保存。但它没有更新。有更好的方法吗 我的职能: exports.invitedSignup = async (req, res, next) => { const { firstName, lastName, company, password, email, companyCode, token } =

我正在尝试在受邀请的用户注册时更新邀请。邀请有一个auth属性,它是一个嵌套对象,它本身有一个键为“used”的属性。我只是尝试使用async/await显式声明该值为true并保存。但它没有更新。有更好的方法吗

我的职能:

exports.invitedSignup = async (req, res, next) =>
{
    const { firstName, lastName, company, password, email, companyCode, token } = req.body;
    console.log(email);
    try
    {
        const user = await User.findOne({ email });
        const invitation = await Invitation.findOne({ email }).sort({ field: 'asc', _id: -1 }).limit(1);
        if (user) { return res.status(422).send({ error: "User is already registered" }); };
        if (!invitation) { return res.status(422).send({ error: "No invitation on record" }); };
        if (token !== invitation.auth.token)
        {
            return res.status(422).send({ error: "Something has gone wrong, please sign up again" });
        }
        try
        {
            invitation.auth.used = true;
            const updateInvitation = await invitation.save();
            console.log("authorization: " + invitation.auth.used);
        } catch (e)
        {
            return next(e);
        }
        try
        {
            const saveUser = new User({
                firstName: firstName,
                lastName: lastName,
                email: req.body.email,
                password: password,
                company: company,
                companyCode: companyCode,
                role: 1,
                auth: { used: true }
            });

            const newUser = await saveUser.save(); 
            const { email, firstname, lastname } = newUser;
            res.json({ token: tokenForUser(newUser), email, firstName, lastName });
        }
        catch (e)
        {
            return next(e);
        }
    }
    catch (e)
    {
        return next(e);
    }
};
邀请架构:

const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const bcrypt = require('bcrypt-nodejs');

//define model
const invitationSchema = new Schema({
    email: { type: String, unique: true, lowercase: true, unique: true },
    inviter: String,
    company: String,
    companyCode: String,
    created: Date,
    auth: {
        token: String,
        used: Boolean,
        expires: Date,
    }
});

invitationSchema.pre('save', function (next)
{
    const invitation = this;

    bcrypt.genSalt(10, (err, salt) =>
    {
        const tomorrow = new Date();
        invitation.created = tomorrow;
        tomorrow.setDate(tomorrow.getDate() + 1);
        if (err) { return next(err); };
        invitation.auth = { token: salt, used: 0, expires: tomorrow };
        next();
    });
});


//create model class
const ModelClass = mongoose.model('invitation', invitationSchema);

//export model
module.exports = ModelClass;


您能否更新您的问题,以包括
邀请
的架构定义?是的,我更新了它您的预保存中间件总是覆盖文档的
auth
子文档,不是吗?@johnyhk啊,这很有意义。因此,我应该重写它,使其仅在邀请没有auth部分时运行,对吗?无论您的业务逻辑需要什么。您是否可以更新您的问题以包括
邀请
的架构定义?是的,我更新了它您的预存中间件总是覆盖文档的
auth
子文档,不是吗?@JohnnyHK ah,这是有道理的。所以我应该重写它,使其仅在邀请没有auth部分的情况下运行,对吗?不管您的业务逻辑需要什么。但是我从未将我的模式类型声明为混合模式。为什么会这样?但我从来没有将我的模式类型声明为混合模式。为什么会这样?
person.anything = { x: [3, 4, { y: "changed" }] };
person.markModified('anything');
person.save(); // anything will now get saved