[英]Mongoose find({}) not returning anything
我想從數據庫中本地分析我的用戶,但是由於某種原因,以下代碼不記錄任何內容,建議嗎?
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);
});
編輯:這是使用的架構:
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);
它與生產中使用的相同,但效果很好
試試這個.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.