我想从我的数据库本地分析我的用户,但出于某种原因,以下代码不记录任何内容,建议?
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);
它与生产中使用的相同,它运作得很好
答案 0 :(得分:0)
试试这个 .exec()返回承诺
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);
});
答案 1 :(得分:0)
你可以这样做,你错过了wirte exec()
async function main() {
const users = await schema.find({}).exec(){
console.log(users.length);
}