如何过滤find FeathersJs中返回的数据
how to filter the data returned in find FeatherJs
我有这个代码 return 这个结果:
// Use this hook to manipulate incoming or outgoing data.
// For more information on hooks, see: http://docs.feathersjs.com/api/hooks.html
module.exports = function (options = {}) { // eslint-disable-line no-unused-vars
return async context => {
// Get `app`, `method`, `params` and `result` from the hook context
const { app, method, result, params } = context;
// Make sure that we always have a list of users either by wrapping
// a single message into an array or by getting the `data` from the `find` method's result
const interacts = method === 'find' ? result.data : [ result ];
// console.log(users);
// Asynchronously get user object from each message's `userId`
// and add it to the message
await Promise.all(interacts.map(async interact => {
// await Promise.all(user.map(async interacts => {
// Also pass the original `params` to the service call
// so that it has the same information available (e.g. who is requesting it)
// bots.botId = await app.service('bots').get(bots.botId, params);
interact.display = await app.service('messages').find({
query: {
to: "ijHTUNeSbSQNICOC",
from: "8Tp5maaTePqiD3DI",
$limit: 1,
$sort: {
createdAt: -1
}
}
});
// }));
}));
console.log(context);
// Best practice: hooks should always return the context
return context;
};
};
return这个结果:
"total": 6,
"limit": 10,
"skip": 0,
"data": [
{
"userId": "8Tp5maaTePqiD3DI",
"interactUser": "ijHTUNeSbSQNICOC",
"_id": "3r3MY8SagJMqcEj4",
"display": {
"total": 2,
"limit": 1,
"skip": 0,
"data": [
{
"text": "nome1",
"from": "8Tp5maaTePqiD3DI",
"to": "ijHTUNeSbSQNICOC",
"createdAt": 1554757301176,
"_id": "o2vkUj7suYzhEZk7"
}
]
}
},
{
"userId": "fNEgNVk6yh3msVa8",
"interactUser": "Z5DO1Gx2YpXLTRBb",
"_id": "KAtOheLC6AEiJL41",
"display": {
"total": 2,
"limit": 1,
"skip": 0,
"data": [
{
"text": "nome1",
"from": "8Tp5maaTePqiD3DI",
"to": "ijHTUNeSbSQNICOC",
"createdAt": 1554757301176,
"_id": "o2vkUj7suYzhEZk7"
}
]
}
},
{
"userId": "Z5DO1Gx2YpXLTRBb",
"interactUser": "Z5DO1Gx2YpXLTRBb",
"_id": "LgoqQxxR8538fnu3",
"display": {
"total": 2,
"limit": 1,
"skip": 0,
"data": [
{
"text": "nome1",
"from": "8Tp5maaTePqiD3DI",
"to": "ijHTUNeSbSQNICOC",
"createdAt": 1554757301176,
"_id": "o2vkUj7suYzhEZk7"
}
]
}
},
{
"userId": "8Tp5maaTePqiD3DI",
"interactUser": "8Tp5maaTePqiD3DI",
"_id": "PuUaonKg3hol2wz0",
"display": {
"total": 2,
"limit": 1,
"skip": 0,
"data": [
{
"text": "nome1",
"from": "8Tp5maaTePqiD3DI",
"to": "ijHTUNeSbSQNICOC",
"createdAt": 1554757301176,
"_id": "o2vkUj7suYzhEZk7"
}
]
}
},
如何只留下数据这..:[=13=]
"userId": "Z5DO1Gx2YpXLTRBb",
"interactUser": "fNEgNVk6yh3msVa8",
"_id": "T3ekz58yNDacS5o2",
"display":
{
"text": "nome1",
"from": "8Tp5maaTePqiD3DI",
"to": "ijHTUNeSbSQNICOC",
"createdAt": 1554757301176,
"_id": "o2vkUj7suYzhEZk7"
}
"userId": "Z5DO1Gx2YpXLTRBb",
"interactUser": "fNEgNVk6yh3msVa8",
"_id": "T3ekz58yNDacS5o2",
"display":
{
"text": "nome1",
"from": "8Tp5maaTePqiD3DI",
"to": "ijHTUNeSbSQNICOC",
"createdAt": 1554757301176,
“_id”:"o2vkUj7suYzhEZk7"
}
如果有任何方法,或者更确切地说是格式化结果 return 由查询
你之所以有这些结果是因为 pagination
。禁用分页只会 return 数据。
您可以创建自己的挂钩来处理将在需要时禁用分页的查询。
好消息是,有一个软件包可以解决您的问题,feahters-hooks-common。
您想要的确切挂钩记录在 (disablepagination) 中。
我有这个代码 return 这个结果:
// Use this hook to manipulate incoming or outgoing data.
// For more information on hooks, see: http://docs.feathersjs.com/api/hooks.html
module.exports = function (options = {}) { // eslint-disable-line no-unused-vars
return async context => {
// Get `app`, `method`, `params` and `result` from the hook context
const { app, method, result, params } = context;
// Make sure that we always have a list of users either by wrapping
// a single message into an array or by getting the `data` from the `find` method's result
const interacts = method === 'find' ? result.data : [ result ];
// console.log(users);
// Asynchronously get user object from each message's `userId`
// and add it to the message
await Promise.all(interacts.map(async interact => {
// await Promise.all(user.map(async interacts => {
// Also pass the original `params` to the service call
// so that it has the same information available (e.g. who is requesting it)
// bots.botId = await app.service('bots').get(bots.botId, params);
interact.display = await app.service('messages').find({
query: {
to: "ijHTUNeSbSQNICOC",
from: "8Tp5maaTePqiD3DI",
$limit: 1,
$sort: {
createdAt: -1
}
}
});
// }));
}));
console.log(context);
// Best practice: hooks should always return the context
return context;
};
};
return这个结果:
"total": 6,
"limit": 10,
"skip": 0,
"data": [
{
"userId": "8Tp5maaTePqiD3DI",
"interactUser": "ijHTUNeSbSQNICOC",
"_id": "3r3MY8SagJMqcEj4",
"display": {
"total": 2,
"limit": 1,
"skip": 0,
"data": [
{
"text": "nome1",
"from": "8Tp5maaTePqiD3DI",
"to": "ijHTUNeSbSQNICOC",
"createdAt": 1554757301176,
"_id": "o2vkUj7suYzhEZk7"
}
]
}
},
{
"userId": "fNEgNVk6yh3msVa8",
"interactUser": "Z5DO1Gx2YpXLTRBb",
"_id": "KAtOheLC6AEiJL41",
"display": {
"total": 2,
"limit": 1,
"skip": 0,
"data": [
{
"text": "nome1",
"from": "8Tp5maaTePqiD3DI",
"to": "ijHTUNeSbSQNICOC",
"createdAt": 1554757301176,
"_id": "o2vkUj7suYzhEZk7"
}
]
}
},
{
"userId": "Z5DO1Gx2YpXLTRBb",
"interactUser": "Z5DO1Gx2YpXLTRBb",
"_id": "LgoqQxxR8538fnu3",
"display": {
"total": 2,
"limit": 1,
"skip": 0,
"data": [
{
"text": "nome1",
"from": "8Tp5maaTePqiD3DI",
"to": "ijHTUNeSbSQNICOC",
"createdAt": 1554757301176,
"_id": "o2vkUj7suYzhEZk7"
}
]
}
},
{
"userId": "8Tp5maaTePqiD3DI",
"interactUser": "8Tp5maaTePqiD3DI",
"_id": "PuUaonKg3hol2wz0",
"display": {
"total": 2,
"limit": 1,
"skip": 0,
"data": [
{
"text": "nome1",
"from": "8Tp5maaTePqiD3DI",
"to": "ijHTUNeSbSQNICOC",
"createdAt": 1554757301176,
"_id": "o2vkUj7suYzhEZk7"
}
]
}
},
如何只留下数据这..:[=13=]
"userId": "Z5DO1Gx2YpXLTRBb",
"interactUser": "fNEgNVk6yh3msVa8",
"_id": "T3ekz58yNDacS5o2",
"display":
{
"text": "nome1",
"from": "8Tp5maaTePqiD3DI",
"to": "ijHTUNeSbSQNICOC",
"createdAt": 1554757301176,
"_id": "o2vkUj7suYzhEZk7"
}
"userId": "Z5DO1Gx2YpXLTRBb", "interactUser": "fNEgNVk6yh3msVa8", "_id": "T3ekz58yNDacS5o2", "display": { "text": "nome1", "from": "8Tp5maaTePqiD3DI", "to": "ijHTUNeSbSQNICOC", "createdAt": 1554757301176, “_id”:"o2vkUj7suYzhEZk7" }
如果有任何方法,或者更确切地说是格式化结果 return 由查询
你之所以有这些结果是因为 pagination
。禁用分页只会 return 数据。
您可以创建自己的挂钩来处理将在需要时禁用分页的查询。
好消息是,有一个软件包可以解决您的问题,feahters-hooks-common。
您想要的确切挂钩记录在 (disablepagination) 中。