我正在尝试使用羽毛从n:m关联中获取数据。我正在为我的前端使用 react 并使用羽毛客户端和插座连接到羽毛。
//connection string where the feathers api is running
const host = 'http://localhost:3030';
const socket = io(host, {
transports: ['websocket'],
forceNew: true
});
// construct feathers app
export const client = feathers()
.configure(hooks())
.configure(auth({ storage: window.localStorage }))
.configure(socketio(socket));
但是当我使用 myService.find(( 时,包含参数会从 hook.params 中删除
function (hook) {
hook.params.sequelize = {
include: [{ model: hook.app.services.users.Model,
as: 'teamOwner'
},{
model: hook.app.services.users.Model,
as: 'Trainer'
},{
model: hook.app.services.users.Model,
as: 'Member'
}
]
};
return Promise.resolve(hook);
}
当我在钩子之前配置它时,它工作正常,但是每次我们使用的服务时,所有表都连接在一起。因此,我想在查询中指定要包含的表。喜欢这个
client.service('team').find({
include: [
{ model:client.service('users').Model,
as: 'teamOwner'
},{
model:client.service('users').Model,
as: 'Trainer'
},{
model:client.service('users').Model,
as: 'Member'
}],
query: {
$sort: {
id: 1
}
}
})
提前致谢
由于客户端和服务器之间只传递params.query
,因此最佳方法通常是创建自定义查询参数,以指示要包含的内容:
function (hook) {
const { $include } = hook.params.query;
// Remove from the query so that it doesn't get included
// in the actual database query
delete hook.params.query.$include;
const sequelize = {
include: []
};
if(Array.isArray($include)) {
$include.forEach(name => {
sequelize.include.push({
model: hook.app.services.users.Model,
as: name
});
});
}
return Promise.resolve(hook);
}
现在,客户端可以在查询中指示他们想要的内容,例如 { query: { $include: [ 'teamOwner', 'Trainer' ] } }