[英]Mongoose - Referencing another model using require
我想做的是更新一個收藏品,獎品,然后根據更新另一個收藏品的結果,聯賽。
我有以下代碼:
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!');
}
});
所以,問題是我看到一個錯誤:
[TypeError: Object #<Object> has no method 'findOneAndUpdate']
我有兩個單獨的文件,以使所有內容都易於管理,因此,我擁有了這個文件priss_api.js和另一個文件leages_api.js,該文件已在架構和聯賽模型中對其進行了定義:
var leagueSchema = new mongoose.Schema({...});
var League = mongoose.model('League', leagueSchema);
我在其他地方使用了相同的樣式,但是由於某些原因,在此文件中我看到了錯誤失敗。 有什么建議嗎?
謝謝你,加里。
如果調用require('./leagues_api')
沒有返回League
模型,則您的module.exports
沒有在該文件中正確設置。
它應如下所示:
module.exports = League;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.