0

I'm very new with Dexie.js and Promises, so, bear with me.

How could I get an entity inside a Map function? I have the following method that's responsible to do a data source lookup within my IndexedDB.

function MapData(data) {
    let mappedItems = {
        items : data.messages.map(function(message) {

            let contact = DataBase.GetContact(message.from);
            return {
                id: message.id,
                dataType: message.messageType, 
                dataFrom: contact.name,
                dataImg: contact.photo,
                dataDate: TimestampToFormatedDate(message.date),
                dataContent : {
                    title : message.content.subject,
                    content: message.content.content,
                    fileList: message.content.fileList,
                    participants: message.participants.map(function (participant) {
                        return ({ name: DataBase.GetContact(participant).name })
                    })
                }
            };
        })
    };
    return mappedItems;
}

In the method DataBase.GetContact, I'd like to connect to database (IndexedDB), search the entity by the id, return it (the object from IndexedDB) and then use the object result to get its values while mapping.

Dan
  • 1,518
  • 5
  • 20
  • 48
  • You should first use forEach to create promise for each message and then in Promise.all set items to the desired array returned by Promise.all – binariedMe Mar 21 '17 at 20:43
  • Sorry @binariedMe I didn't understand what you said. Do you have an example? – Dan Mar 21 '17 at 21:13
  • The issue you're facing is that your code is not set to handle asynchronous responses. You should start with tackling that and modify your code to deal with the asynchronous nature of Dexie. Once that's done maybe there will remain a Dexie-specific question. – Louis Mar 23 '17 at 10:54

0 Answers0