簡體   English   中英

貓鼬隨機查詢問題

[英]Mongoose Random Query issues

我試圖弄清楚為什么我正在編寫的貓鼬查詢總是返回null。 不是像往常一樣[],而是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" } }

我的angular 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);
            });
    });
}

對此的任何幫助將不勝感激,因為我敢肯定我在做一些愚蠢的事情,只是我缺乏貓鼬的經驗來弄清楚。

我無法正確傳遞變量,因此端點正確返回了空值或[]。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM