[英]FeathersJs LEFT join another table (MongoDb)
I have a model for a service listings like我有一个 model 用于服务列表,例如
price: { type: Number, required: true, min: 0, default: 0 },
itemId: {
type: mongooseClient.Schema.Types.ObjectId,
ref: 'items',
required: true,
},
and with the after all hook I am doing a fastResolve with毕竟我正在做一个 fastResolve 的钩子
joins: {
items: () => async (instance, context) => {
if (instance && instance.itemId) {
const item = await context.app
.service('items')
.get(instance.itemId);
instance.item = item;
}
return context;
},
so in the end when I do a find for listings, I get the item object.所以最后当我查找列表时,我得到了 object 项目。 And while I can easily query for fields like price (listings?price%5B%24lt%5D=5000)
虽然我可以轻松查询价格等字段(listings?price%5B%24lt%5D=5000)
How should I query for inner fields of item, like weight?我应该如何查询项目的内部字段,比如重量? (when doing a listings find)
(在进行列表查找时)
const populate = (context) => {
const stringList = [
'vehicleId',
'userId',
'locationId',
];
context.params.query['$populate'] = {
path: `${stringList.join(' ')}`,
populate: [
{
path: 'modelId',
model: 'models',
populate: {
path: 'makeId',
model: 'makes',
},
},
{
path: 'userId',
model: 'users',
},
],
};
return context;
};
Based on previous answer managed to do a research and ended up writing the following hook as the solution, for nested populate in FeathersJs基于先前的答案设法进行研究并最终编写了以下钩子作为解决方案,用于 FeathersJs 中的嵌套填充
you can directly pass a $populate
query on the front end.您可以直接在前端传递
$populate
查询。
for eg: ?$populate=itemId
, here "itemId" is the data model's key which is referred to items model.例如:
?$populate=itemId
,这里的“itemId”是数据模型的键,指的是项目 model。
If you want to populate in the backend, you can create a custom hook for that.如果你想在后端填充,你可以为此创建一个自定义钩子。
// src/hooks/setDefaultQuery.js
const setDefaultQuery = (fieldName, defaultValue) => (context) => {
const { params } = context;
const { query } = params;
if (typeof query[fieldName] === 'undefined') context.params.query[fieldName] = defaultValue;
return context;
};
module.exports = setDefaultQuery;
on hooks, you can use the setDefaultQuery
like this在挂钩上,您可以像这样使用
setDefaultQuery
{
before: {
all: [authenticate('jwt')],
find: [setDefaultQuery('$populate', "itemId")],
get: [],
create: [],
update: [],
patch: [],
remove: [],
},
}
For more about the querying please check the link bellow https://docs.feathersjs.com/api/databases/querying.html有关查询的更多信息,请查看下面的链接https://docs.feathersjs.com/api/databases/querying.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.