I cannot understand how promise works. I always receiving an empty list from my promise (userConversationsIDs). My code:
const userConversationsIDs = ;
var userConversations = new Promise((resolve, reject) => {
recipientsList.forEach( async (recipientID) => {
// Create user conversations for the booking
const UserConversation = Parse.Object.extend(‘UserConversations’);
const userConversation = new UserConversation();
console.log(‘*** Creating user conversation for recipientID: ‘, recipientID);
userConversation.set(‘conversationID’, mainConversation.id);
userConversation.set(‘userID’, recipientID);
userConversation.set(‘messages’, );
userConversation.set(‘lastMessage’, ‘’);
userConversation.set(‘lastMessageTime’, new Date());
userConversation.set(‘bookingID’, request.object.id);
// check if the user is client
if (recipientID != clientID) {
// Create owner conversation(s)
console.log(’*** Recipient is not a client:’, recipientID );
userConversation.set(‘userIDs’, [recipientID, clientID]);
const recipientQuery = new Parse.Query(Parse.User);
const currentRecipient = await recipientQuery.get(recipientID, { useMasterKey: true });
const ownerFirstName = currentRecipient.get(‘firstName’, { useMasterKey: true } );
const ownerLastName = currentRecipient.get(‘lastName’, { useMasterKey: true } );
const ownerName = ownerFirstName + ’ ’ + ownerLastName;
console.log(‘*** Owner name for owner conversation is: ‘, ownerName);
userConversation.set(‘userNames’, [ownerName, clientName]);
var response = await userConversation.save(null, { useMasterKey: true } );
console.log(’*** Response ID in save is: ‘, response.id);
userConversationsIDs.push(response.id);
} else {
// Create client conversation
console.log(’*** Recipient is a client:’, recipientID );
userConversation.set(‘userIDs’, [clientID, currentOwner.id]);
const ownerFirstName = currentOwner.get(‘firstName’, { useMasterKey: true } );
const ownerLastName = currentOwner.get(‘lastName’, { useMasterKey: true } );
const ownerName = ownerFirstName + ’ ’ + ownerLastName;
console.log('*** Owner name for client user conversation is: ‘, ownerName);
userConversation.set(‘userNames’, [clientName, ownerName]);
var response = await userConversation.save(null, { useMasterKey: true } );
console.log(’*** Response ID in save is: ‘, response.id);
userConversationsIDs.push(response.id);
}
});
console.log(’*** Got user conversations list: ', userConversationsIDs);
});
And response in log is:
*** Creating user conversation for recipientID: spNUyASEtw
*** Recipient is a client: spNUyASEtw
*** Owner name for client user conversation is: Baja Main Owner
*** Creating user conversation for recipientID: HnsCdWNCyR
*** Recipient is not a client: HnsCdWNCyR
*** Creating user conversation for recipientID: HTQ1bJX8sp
*** Recipient is not a client: HTQ1bJX8sp
*** Creating user conversation for recipientID: AAoGRhbbq0
*** Recipient is not a client: AAoGRhbbq0
*** Got user conversations list:
info: afterSave triggered for Bookings for user spNUyASEtw:
Input: {“postingID”:“2wjnv1mbg8”,“dates”:[{“__type”:“Date”,“iso”:“2022-01-19T10:45:00.000Z”},{“__type”:“Date”,“iso”:“2022-01-20T10:45:00.000Z”},{“__type”:“Date”,“iso”:“2022-01-21T10:45:00.000Z”},{“__type”:“Date”,“iso”:“2022-01-22T10:45:00.000Z”}],“deliveryAddress”:“Test Address”,“helmetsNumber”:1,“clientID”:“spNUyASEtw”,“ownerID”:“AAoGRhbbq0”,“isAccepted”:false,“isDeclined”:false,“isCompleted”:false,“createdAt”:“2022-01-19T10:30:54.051Z”,“updatedAt”:“2022-01-19T10:30:54.051Z”,“objectId”:“2bxF5KI8Mh”}
Result: undefined {“className”:“Bookings”,“triggerType”:“afterSave”,“user”:“spNUyASEtw”}
*** Owner name for owner conversation is: Test1 Owner
*** Owner name for owner conversation is: Test2 Owner
*** Owner name for owner conversation is: Baja Main Owner
*** Response ID in save is: ejoeTz131G
*** Response ID in save is: YicjkWMAwq
*** Response ID in save is: 8mZR6QV4O8
*** Response ID in save is: sBbO73NEFM