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
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); }
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.