猫鼬findOneAndUpdate嵌入式文档不会引发错误

时间:2019-10-06 11:03:29

标签: javascript mongoose

我现在正在寻找解决方案2天,但找不到任何东西。我有一个具有2个嵌入式方案的模式。我希望其中一种嵌入式方案是唯一的。我尝试过预保存中间件,但不能正常运行。

现在,我在schema.save()函数之前添加了两个条件以进行验证,但它们都不起作用。它们都将失败消息发送回客户端,但是schema.save函数将运行并插入新文档!

在运行该功能的schema.save()之前,我尝试使用try-catch,但是代码永远不会抛出错误!

谢谢!

我要附加模式和控制器脚本!

这是topic.js,架构文件

var mongoose = require('mongoose');

var Bookmarks = mongoose.Schema({
    title: {
        type: String,
        required: true
    },
    description: {
        type: String, 
        required: true
    },
    link: {
        type: String,
        trim: true,
        unique: true,
        required: true
    },
    owner : {
        type: [String],
        trim: true,
        required: true
    },
    tags: {
        type: [String],
        trim: true    
    },
    image : {
        type: String,
        trim: true
    },
    created_at: {
        type: Date,
        default: Date.now,
        required: true
    },
    modified_at: {
        type: Date,
        default: Date.now,
        required: true
    }
},{
    versionKey: false // You should be aware of the outcome after set to false
});

var Groups = mongoose.Schema({
    name: {
        type: String,
        required: true
    },
    description: {
        type: String, 
        required: true
    },
    admin: {
        type: String,
        trim: true,
        required: true
    },
    members: {
        type: [String],
        trim: true    
    },
    bookmarks: {
        type: [Bookmarks]
    }
},{
    versionKey: false // You should be aware of the outcome after set to false
});

// Setup Schema
var Topics = mongoose.Schema({
    name: {
        type: String,
        required: true,
        lowercase: true,
        unique: true
    },
    // Group Types
    groups: {
        type: [Groups]
    },
    // Bookmarks type
    bookmarks: {
        type: [Bookmarks],
        unique:true
    }   
},{
    versionKey: false // You should be aware of the outcome after set to false
});

// Export User model
module.exports = mongoose.model('topic', Topics);

controller.js文件

// Add Bookmark on Topic
exports.addBookmark = function(req, res) {
    Topic.findOneAndUpdate( { name: req.body.name} , { $addToSet: { bookmarks: req.body.bookmark } } , {upsert: true }, function(err, topic) {
        if (err) {
            res.json({
                status: 400,
                message: "This topic does not exists"
            });
        } else if ( topic == null || req.body.bookmark == null) {
             res.json({
                status: 400,
                message: "Please, complete all fields."
            });
        } else {

            let bookmark = req.body.bookmark; 
            let flag = false;

            for (var i = 0; i < topic.bookmarks.length ; i++) {
                if ( topic.bookmarks[i].link === req.body.bookmark.link ) {
                    flag = true;
                    res.json({
                        status: 400,
                        message: "This bookmark already exists on this topic"
                    });
                    return err;
                }
            }

            if ( bookmark.title == "" || bookmark.title == undefined  || bookmark.description == "" || bookmark.description == undefined || bookmark.link == "" || 
                bookmark.link == undefined || bookmark.owner == "" || bookmark.owner == undefined ) {
                   res.json({
                        status: 400,
                        message: "Please, complete all required fields for bookmark."
                    });
                return;
            }

            topic.save(function(err, flag) {
                if (err) {
                    res.json({
                        status: 400,
                        message: err,
                    });
                } else {
                    res.json({
                        status: 200,
                        message: "Bookmark added to your topic list."
                    });
                }
            });
        }
    });
};

0 个答案:

没有答案