简体   繁体   English

猫鼬find({})不返回任何内容

[英]Mongoose find({}) not returning anything

I want to locally analyze my users from my database, but for some reason, the following code does not log anything, suggestions? 我想从数据库中本地分析我的用户,但是由于某种原因,以下代码不记录任何内容,建议吗?

const mongoose = require('mongoose');
const mongoURIDEV = 'mongodb://MY-URI';
const MONGO = mongoURIDEV;

mongoose.connect(MONGO);
const schema = require('../schema/users');

async function main() {
const users = await schema.find({});
    console.log(users.length);
}

main().catch((e) => {
    console.log(e);
});

edit: here is the schema being used: 编辑:这是使用的架构:

const mongoose = require('mongoose');

const { Schema } = mongoose;

const usersSchema = new Schema({
  id: String,
  uid: String,
  gender: String,
  forward: String,
  keyword: String,
  placeType: String,
  profile_pic: String,
  referenceName: String,
  order_restaurant: String,
  order_people: String,
  order_time: String,
  order_timestamp: Object,
  order_email: String,
  order_phone: String,
  order_contact: []
}, { collection: 'Users' });

module.exports = mongoose.model('Users', usersSchema);

it is the same one used in production is it works just fine 它与生产中使用的相同,但效果很好

try this .exec() returns promise 试试这个.exec()返回promise

async function main() {
const users = await schema.find({}).exec();
    console.log(users.length);
    return users;
}

main().then((data) => {
    console.log(data);
}).catch((e) => {
    console.log(e);
});

您可以做到这一点,而您想错过exec()

async function main() { const users = await schema.find({}).exec(){ console.log(users.length); }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM