У меня проблема при попытке получить все сообщения от всех пользователей в коллекции контактов.
Сначала. я делаю запрос в коллекцию пользователей, чтобы получить идентификатор пользователя, после этого. используя этот идентификатор пользователя, чтобы получить сообщение из коллекции сообщений.
в моем =>
PostController.js
// Get post status
let getPosts = await post.getPosts(req.user._id);
PostServices.js
let LIMIT_CONTACT = 3;
let getPosts = (currentUserId) => {
return new Promise(async (resolve, reject) => {
let contacts = await ContactModel.getContacts(currentUserId, LIMIT_CONTACT);
let getID = contacts.map(async contact => {
if (contact.contactId == currentUserId) {
let getUser = await UserModel.findNormalDataByID(contact.userId);
return getUser;
} else {
let getUser = await UserModel.findNormalDataByID(contact.contactId);
return getUser;
}
});
let contactId = await Promise.all(getID);
let getPostById = contactId.map(async contact => {
return await PostModel.getPost(contact._id);
});
resolve(await Promise.all(getPostById));
});};
PostModel. js
getAllPosts(currentUserId) {
return this.find({ "postUserId": currentUserId }).sort({ "createdAt": -1 }).exec();
},
getPost(postID) {
return this.find({ "postUserId": postID }).exec();
},
Ожидаемые данные, подобные этой
[{ numOfLikes: 1,
numOfComments: 0,
status: 'public',
editedAt: null,
deletedAt: null,
_id: 5d9551f793ad0609f82f397f,
postUser: 'Hai Loc',
postUserId: '5d46b7e5e08f4222384259b9',
avatar:
'1566978062948-2a5589c9-733f-48ff-9be4-d4f542946c1f-storm view.jpg',
postContent:
'sample post content',
comments: [],
createdAt: 1570066935792,
__v: 0 },
{ numOfLikes: 2,
numOfComments: 0,
status: 'public',
editedAt: null,
deletedAt: null,
_id: 5d95549693ad0609f82f3980,
postUser: 'Hai Loc',
postUserId: '5d46b7e5e08f4222384259b9',
avatar:
'1566978062948-2a5589c9-733f-48ff-9be4-d4f542946c1f-storm view.jpg',
postContent: 'create new post',
comments: [Array],
createdAt: 1570067606887,
__v: 0 }]
Но получили этот тип с несколькими квадратными скобками
[ [ { numOfLikes: 1,
numOfComments: 0,
status: 'public',
editedAt: null,
deletedAt: null,
_id: 5d9551f793ad0609f82f397f,
postUser: 'Hai Loc',
postUserId: '5d46b7e5e08f4222384259b9',
avatar:
'1566978062948-2a5589c9-733f-48ff-9be4-d4f542946c1f-storm view.jpg',
postContent:
'sample post content',
comments: [],
createdAt: 1570066935792,
__v: 0 },
{ numOfLikes: 2,
numOfComments: 0,
status: 'public',
editedAt: null,
deletedAt: null,
_id: 5d95549693ad0609f82f3980,
postUser: 'Hai Loc',
postUserId: '5d46b7e5e08f4222384259b9',
avatar:
'1566978062948-2a5589c9-733f-48ff-9be4-d4f542946c1f-storm view.jpg',
postContent: 'create new post',
comments: [Array],
createdAt: 1570067606887,
__v: 0 } ],
[],
[],
[],
[],
[],
[],
[],
[],
[] ]
Пожалуйста, помогите, что я делаю не так с Promise.all (неверный формат json)