简体   繁体   English

使用搜索查询返回结果猫鼬v4

[英]Return results mongoose v4 with find query

I have left my code for over a year and have ran NPM install and since it obviously has changed :) 我已经离开我的代码一年多了,已经安装了NPM,因为它显然已经改变了:)

My Query 我的查询

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); console.info(clubs); Output 输出量

在此处输入图片说明

Here you can see that it is returning the model rather than the results of the clubs. 在这里,您可以看到它返回的是模型,而不是俱乐部的结果。

My model 我的模特

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);

How can I get it return a list of the clubs? 我如何获得俱乐部的名单?

The clubs table is full of match data. 俱乐部表中充满了比赛数据。 But I used to get a return from 'name teamID date norseman eleven' now I just get a return of :\\ 但是我曾经从'name teamID date norseman eleven'那里获得回报,现在我得到了:\\

**Using exec() **使用exec()

在此处输入图片说明

You need to execute your find mongoose query method, by trailing your query with exec() method so that it return a Promise with your list values resolved, so change the following line: 您需要执行find猫鼬查询方法,方法是使用exec()方法跟踪查询,以使其返回已解决列表值的Promise ,因此更改以下行:

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

to: 至:

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