0
我想弄清楚爲什麼我寫的貓鼬查詢總是返回null。不是[]像往常一樣,但爲空。貓鼬隨機查詢問題
下面是代碼:
// Get random single opponent within 300 points of your team value in either direction
app.get('/api/teams/getOpponent', function (req, res) {
var value = req.query.teamValue;
var owner = req.query.owner;
var maxVal = value + 300;
var minVal = value - 300;
var filter = {
value: { $gte: minVal, $lte:maxVal },
owner: { $ne: owner }
};
var fields = {}
var options = { limit: 1 }
// use mongoose to get all teams in the database
Team.findRandom(filter, fields, options, function (err, team) {
// if there is an error retrieving, send the error. nothing after res.send(err) will execute
if (err)
res.send(err)
res.json(team); // return a team in JSON format
});
});
所以基本上在這裏發生的事情是一個文檔_id得到傳過來的還有一個數字。然後,我想要抓住一個隨機的團隊文檔,其中一個團隊在當前用戶團隊的300個點內,並且所有者不等於當前用戶,因此他們不會最終挑戰他們自己的團隊。
古怪足夠當我輸入下面的查詢到mongoCompass它返回預期的結果
{ "value": { $gte:1000, $lte:1600}, "owner": { $ne: "58986cd25e7c780011881bcd" } }
我的角2提供商尋找使API調用如下。
getOpponent(options) {
console.log("options", options);
return new Promise(resolve => {
this.http.get('https://pitchlife-hearts.herokuapp.com/api/teams/getOpponent?teamValue=' + options.value + '&owner=' + options.owner)
.map(res => res.json())
.subscribe(data => {
this.data = data;
resolve(this.data);
});
});
}
任何幫助,這將是我敢肯定我做一些愚蠢的事我只是缺乏與貓鼬的經驗弄明白非常感激。