我想从feathers.js钩子中的集合中获取信息。 如何让挂钩等待,直到mongodb调用完成?在它发出的钩无需等待呼叫到结束的那一刻,我想它的回报和promieses,但是毫无效果MongoDB在feathers.js中调用钩子
// 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;
});
})
})
};
非常感谢,我认为这个解决方案比较好,但是我需要移动'return new Promise((resolve,reject)=> {'为'MongoClient.connect') – Hydish
好点,我修改了代码示例 – Daff