我想从一个 feathers.js 钩子中的集合中获取信息。我怎样才能让钩子等待,直到 mongodb 调用完成?目前它发送钩子而不等待调用完成,我尝试了返回和承诺,但没有任何效果
// Connection URL
const url = 'mongodb://localhost:27017/db';
//Use connect method to connect to the server
module.exports = function(hook) {
MongoClient.connect(url, function(err, db) {
const userCollection = db.collection('question');
userCollection.count().then(function(N) {
const R = Math.floor(Math.random() * N)
const randomElement = userCollection.find().limit(1).skip(R).toArray(function(err, docs) {
console.log("Found the following records");
console.log(docs)
//update hook with data from mongodb call
hook.data.questionid = docs._id;
});
})
})
};