conversation.user和conversation.secondUser附加到会话对象,但嵌套的消息循环在响应发送到客户端后执行。
find: [
async (context) => {
await Promise.all(context.result.data.map((conversation) => {
return context.app.service('users').get(conversation.userId).then((data) => {
conversation.user = data;
return context.app.service('users').get(conversation.secondUserId).then((data) => {
conversation.secondUser = data;
return conversation.messages.map((message) => {
return context.app.service('users').get(message.userId).then((data) => {
console.log(data);
message.user = data;
});
});
});
});
}));
context.dispatch = context.result;
return context;
}
],
答案 0 :(得分:1)
两件事:
Promise.all
async/await
这应该有效:
find: [
async (context) => {
await Promise.all(context.result.data.map(async (conversation) => {
const data = await context.app.service('users').get(conversation.userId);
const secondData = await context.app.service('users').get(conversation.secondUserId);
conversation.user = data;
conversation.secondUser = secondData;
await Promise.all(conversation.messages.map(async (message) => {
const data = await context.app.service('users').get(message.userId);
console.log(data);
message.user = data;
}));
}));
context.dispatch = context.result;
return context;
}
]