我是Feathersjs的新手。请帮助我理解这些内容
所以,我有这个代码(products.services.js)
function mapUserIdToData(context) {
if (context.data && context.params.route.userId) {
context.data.user = context.params.route.userId;
}
}
app.use("/stores", new Stores(options, app));
const service = app.service("stores");
service.hooks(hooks);
// setup our nested routes
app.use("/users/:userId/stores", app.service("stores"));
app.service("users/:userId/stores").hooks({
before: {
find(context) {
if (context.params.route.userId)
context.params.query.user = context.params.route.userId;
},
create: mapUserIdToData,
update: mapUserIdToData,
patch: mapUserIdToData,
},
});
在我的注册模块(register.service.js)中,我调用了这些逻辑来为新用户创建商店
const users = this.app.service("users");
const user = await users.create(userData);
// save store
if (storeTitle) {
const stores = this.app.service("stores");
await stores.create({ user: user._id, title: storeTitle });
}
我不明白的是:为什么行await stores.create({ user: user._id, title: storeTitle });
也会触发app.service("users/:userId/stores")
中的钩子逻辑?我知道,因为它运行mapUserIdToData
函数,因为没有userId
的路由参数,该函数将失败。
谢谢