简体   繁体   English

猫鼬-使用require引用其他模型

[英]Mongoose - Referencing another model using require

What I'm trying to do is to update one collection, Prize, and then based on the result from that update another collection, League. 我想做的是更新一个收藏品,奖品,然后根据更新另一个收藏品的结果,联赛。

I have the following code: 我有以下代码:

app.post('/auth/prize/:prizeId', function(req, res) {
    console.log('POST /auth/prize/' + req.params.prizeId);
    if (req.user) {
        var mess = 'JOINED';
        var query = {...};
        var update = {...};
        Prize.findOneAndUpdate(query, update, function(err, result) {
            if (err || result === null) {
                mess = 'ERROR Incorrect password';
                res.send(mess);
                return;
            }
            var League = require('./leagues_api');
            var queryL = {...};
            var updateL = {...};
            League.findOneAndUpdate(queryL, updateL, function(e, r) {
                if (e || r === null) {
                    mess = 'ERROR Incorrect password';
                }
                res.send(mess);
            });
        });
    } else {
        res.send(401, 'Not Admin!');
    }
});

So, the issue is that I am seeing an error: 所以,问题是我看到一个错误:

[TypeError: Object #<Object> has no method 'findOneAndUpdate']

I have two separate files to keep everything easy to manage, so I have this file, prizes_api.js, and another file, leages_api.js, which has defined it in the Schema and the Model for League: 我有两个单独的文件,以使所有内容都易于管理,因此,我拥有了这个文件priss_api.js和另一个文件leages_api.js,该文件已在架构和联赛模型中对其进行了定义:

var leagueSchema = new mongoose.Schema({...});
var League = mongoose.model('League', leagueSchema);

I use this same style elsewhere, but for some reason in this file I am seeing the error failure. 我在其他地方使用了相同的样式,但是由于某些原因,在此文件中我看到了错误失败。 Any advice on this please? 有什么建议吗?

Thank you, Gary. 谢谢你,加里。

If calling require('./leagues_api') isn't returning the League model, then your module.exports isn't set up right in that file. 如果调用require('./leagues_api')没有返回League模型,则您的module.exports没有在该文件中正确设置。

It should look like the following: 它应如下所示:

module.exports = League;

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

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