我正在尝试在羽毛钩子中发出自定义事件。我在钩子中尝试了以下内容:
module.exports = function (options = {}) {
return async context => {
const { app } = context;
const groupsService = app.service('groups');
const groups = await groupsService.find( { query:{ members: context.result.user._id } });
await app.emit('list', groups); // Try 1
await groupsService.emit('list', groups); // Try 2
await context.service.emit('list', groups); // Try3
console.log('====================EMIT====================');
return context;
};
};
我还将事件添加到服务中:
module.exports = function (app) {
this.events = ['list']; // Try 1
(...)
// Get our initialized service so that we can register hooks
const service = app.service('groups');
service.events = ['list']; // Try 2
service.hooks(hooks);
};
问题是,事件永远不会发出。通过调试,我只能在控制台中看到以下消息:
debug: 在 app.service('groups'(.find(( 之前
debug: after app.service('groups'(.find((
===================EMIT==
========================
但是,永远不会使用 Socket.io 发出事件。在频道中,我将每个事件发送给经过身份验证的用户。我也尝试将其更改为每个人,但没有成功。当我使用DEBUG=* npm start
运行 Feathers 时,我可以看到,该事件永远不会发出。知道吗?谢谢!
目前这仍然有点挑剔。问题是调用events
数组时需要位于服务上app.use
。这可以像这样完成:
module.exports = function (app) {
(...)
app.use('/groups', Object.assign(createService(options), {
events: [ 'list' ]
}));
// Get our initialized service so that we can register hooks
const service = app.service('groups');
service.hooks(hooks);
};
然后,您可以按照文档使用它:
groupsService.emit('list', groups)
Feathers数据库服务已经采用了events
选项,因此它看起来像这样:
module.exports = function (app) {
const options = {
(...)
events: [ 'list' ]
};
app.use('/groups', createService(options));
// Get our initialized service so that we can register hooks
const service = app.service('groups');
service.hooks(hooks);
};