簡體   English   中英

使用搜索查詢返回結果貓鼬v4

[英]Return results mongoose v4 with find query

我已經離開我的代碼一年多了,已經安裝了NPM,因為它顯然已經改變了:)

我的查詢

var mongoose = require('mongoose'),
    Clubs = mongoose.model('Clubs');

getNorsemanClubs: function(passData){
    return new Promise(function (resolve) {
        Clubs.find({norseman:true}, 'name teamID date norseman eleven').sort({ eleven : 'asc'})
            .then(function(clubs){
                console.info(clubs);
                passData.clubs = clubs;
                resolve(passData);
            });
    })
}

console.info(clubs); 輸出量

在此處輸入圖片說明

在這里,您可以看到它返回的是模型,而不是俱樂部的結果。

我的模特

var mongoose = require('mongoose')
    , Schema = mongoose.Schema;

/**
 * User Schema
 */

var ClubsScheme = new Schema({
    name: String,
    teamID: { type: String, default: null },
    date: { type: Date, default: Date.now},
    norseman: {type: Boolean, default: false},
    eleven: Number
})

mongoose.model('Clubs', ClubsScheme);

我如何獲得俱樂部的名單?

俱樂部表中充滿了比賽數據。 但是我曾經從'name teamID date norseman eleven'那里獲得回報,現在我得到了:\\

**使用exec()

在此處輸入圖片說明

您需要執行find貓鼬查詢方法,方法是使用exec()方法跟蹤查詢,以使其返回已解決列表值的Promise ,因此更改以下行:

Clubs.find({norseman:true}, 'name teamID date norseman eleven').sort({ eleven : 'asc'})

至:

Clubs.find({norseman:true}, 'name teamID date norseman eleven').sort({ eleven : 'asc'}).exec()

暫無
暫無

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

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