FeathersJS通过钩子续集调用存储过程无法解析



我在创建调用存储过程的钩子时遇到问题。

我的自定义服务钩子(customservice.hooks.js(是:

const callstored = require('../../hooks/callstored')
module.exports = {
before: {
all: [],
find: [callstored()],
get: [],
create: [],
update: [],
patch: [],
remove: []
},
...

我的服务类别(定制服务.class.js(:

const mysql = require('mysql')
class Service {
constructor (options) {
this.options = options || {};
}
find (params) {
return Promise.resolve( [] );
}
get (id, params) {
return Promise.resolve({
id, text: `A new message with ID: ${id}!`
});
}
create (data, params) {
if (Array.isArray(data)) {
return Promise.all(data.map(current => this.create(current)));
}
return Promise.resolve(data);
}
update (id, data, params) {
return Promise.resolve(data);
}
patch (id, data, params) {
return Promise.resolve(data);
}
remove (id, params) {
return Promise.resolve({ id });
}
}
module.exports = function (options) {
return new Service(options);
};
module.exports.Service = Service;

和我的服务(customservice.service.js(:

const createService = require('./rankingvotes.class.js');
const hooks = require('./rankingvotes.hooks');
module.exports = function (app) {
const paginate = app.get('paginate');
const options = {
name: 'rankingvotes',
paginate
};
// Initialize our service with any options it requires
app.use('/rankingvotes', createService(options));
// Get our initialized service so that we can register hooks and filters
const service = app.service('rankingvotes');
service.hooks(hooks);
app.publish(() => {
});
};

最后我的钩子是(调用存储.js(

const Sequelize = require('sequelize');
module.exports = function () {
return function (hook) {
sequelize = hook.app.get('sequelizeClient');
let result=[];
return sequelize.query('CALL RANKING();',{
nest: true,
raw: true }).then(function(response){
console.log(response[0]) //data are correct
hook.data=response[0]; 
return hook;
}).error(function(err){
console.log(err);
return hook;
});
}
}

如果我检查控制台,我正确获取了数据,但没有从 Postman 等 REST 客户端调用任何内容。

知道吗?谢谢。

如果你想改变响应,你必须设置 hook.result。

const Sequelize = require('sequelize');
module.exports = function () {
return async hook => {
try {
const sequelize = hook.app.get('sequelizeClient');
const response = await sequelize.query('CALL RANKING();',{
nest: true,
raw: true
});
console.log(response[0]) //data are correct
context.result = response[0]; 
} catch(error) {
console.error(error);
}
return hook;
}
}

hook.data 是请求数据,仅适用于createupdatepatch

请记住,如果在before挂钩中设置自定义服务,find设置hook.result跳过自定义服务。

相关内容

  • 没有找到相关文章

最新更新