MongoDB 内部调用 feathers.js Hook
MongoDB call inside feathers.js Hook
我想从 feathers.js 挂钩中的集合中获取信息。
如何让挂钩等待 mongodb 调用完成?目前它没有等待调用完成就发送了挂钩,我用 returns 和 promieses 尝试了它,但没有任何效果
// 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;
});
})
})
};
You can use async.waterfall() of async module
const async=require('async');
async.waterfall([function(callback) {
userCollection.count().then(function(N) {
callback(null, N);
});
}, function(err, N) {
if (!err) {
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;
});
}
}])
解决问题的方法是使用
module.exports = function(hook, next) {
//insert your code
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");
hook.data.questionid = docs[0].email;
//after all async calls, call next
next();
});
}
理想的方法是使hook asynchronous and return a Promise与钩子对象解析:
// Connection URL
const url = 'mongodb://localhost:27017/db';
const connection = new Promise((resolve, reject) => {
MongoClient.connect(url, function(err, db) {
if(err) {
return reject(err);
}
resolve(db);
});
});
module.exports = function(hook) {
return connection.then(db => {
const userCollection = db.collection('question');
return userCollection.count().then(function(N) {
const R = Math.floor(Math.random() * N);
return new Promise((resolve, reject) => {
userCollection.find().limit(1)
.skip(R).toArray(function(err, docs) {
if(err) {
return reject(err);
}
hook.data.questionid = docs._id;
resolve(hook);
});
});
});
});
});
};
Daff 的解决方案对我不起作用。我收到以下错误:
info: TypeError: fn.bind is not a function
解决方法是:好像普通的钩子可以用括号注册,但是这个钩子必须不带括号注册。 寻找敌人
exports.before = {
all: [
auth.verifyToken(),
auth.populateUser(),
auth.restrictToAuthenticated()],
find: [],
get: [],
create: [findEnemy],
update: [],
patch: [],
remove: []
};
findEnemy() 不起作用。也许其他人 运行 遇到了同样的问题。有人可以解释为什么吗?
我想从 feathers.js 挂钩中的集合中获取信息。 如何让挂钩等待 mongodb 调用完成?目前它没有等待调用完成就发送了挂钩,我用 returns 和 promieses 尝试了它,但没有任何效果
// 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;
});
})
})
};
You can use async.waterfall() of async module
const async=require('async');
async.waterfall([function(callback) {
userCollection.count().then(function(N) {
callback(null, N);
});
}, function(err, N) {
if (!err) {
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;
});
}
}])
解决问题的方法是使用
module.exports = function(hook, next) {
//insert your code
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");
hook.data.questionid = docs[0].email;
//after all async calls, call next
next();
});
}
理想的方法是使hook asynchronous and return a Promise与钩子对象解析:
// Connection URL
const url = 'mongodb://localhost:27017/db';
const connection = new Promise((resolve, reject) => {
MongoClient.connect(url, function(err, db) {
if(err) {
return reject(err);
}
resolve(db);
});
});
module.exports = function(hook) {
return connection.then(db => {
const userCollection = db.collection('question');
return userCollection.count().then(function(N) {
const R = Math.floor(Math.random() * N);
return new Promise((resolve, reject) => {
userCollection.find().limit(1)
.skip(R).toArray(function(err, docs) {
if(err) {
return reject(err);
}
hook.data.questionid = docs._id;
resolve(hook);
});
});
});
});
});
};
Daff 的解决方案对我不起作用。我收到以下错误:
info: TypeError: fn.bind is not a function
解决方法是:好像普通的钩子可以用括号注册,但是这个钩子必须不带括号注册。 寻找敌人
exports.before = {
all: [
auth.verifyToken(),
auth.populateUser(),
auth.restrictToAuthenticated()],
find: [],
get: [],
create: [findEnemy],
update: [],
patch: [],
remove: []
};
findEnemy() 不起作用。也许其他人 运行 遇到了同样的问题。有人可以解释为什么吗?