Search code examples
javascriptnode.jsnpmpromisebluebird

How to Retrieve 3 records from storages and put them into dbResult using promise join


How to use promise join when retrieving 3 records and put into dbResult? Currently, I have one record retrieve like following code,

 req.oracleMobile.storage.getById(registry.getIncidentPhotoStorageName(), incident_id + '_01', { sync: true }).then(
            function (result) {
                base64data = result.result;
                base64data = JSON.parse(base64data);
                dbResult.photo = base64data.image;

                res.status(result.statusCode).send(dbResult);
    },
            function (err) {
               dbResult.photo = imageData.uploadPhotoIcon;
                //ignore no photo and send db result
                res.status(200).send(dbResult);
            }
        );

I want to retrieve 3 records and add it like dbResult.photo2 = base64data.image; using promise join . I have tried " return promise.join(retrieve1,retrieve2,retrieve3 );". it doesn't work. This is my code...

function getIncident(req, res) {
getIncidentRow(req).then(
    function (dbResult) {
        var incident_id = dbResult.id;

        const join = require('promise-join');
        return join(req.oracleMobile.storage.getById(registry.getIncidentPhotoStorageName(), incident_id + '_01',  { sync: true }).then(
            function (result) {
                base64data = result.result;
                base64data = JSON.parse(base64data);
                dbResult.photo = base64data.image;
                //res.status(result.statusCode) .send(dbResult);                                 
            },
            function (err) {
                //ignore no photo and send db result
                res.status(200).send(dbResult);
            }
        ) ,req.oracleMobile.storage.getById(registry.getIncidentPhotoStorageName(), incident_id + '_02',  { sync: true }).then(
            function (result) {
                base64data = result.result;
                base64data = JSON.parse(base64data);
                dbResult.photo2 = base64data.image;
               // res.status(result.statusCode).send(dbResult);                 
            },
            function (err) {  
                //ignore no photo and send db result
                res.status(200).send(dbResult);
            }
        ) ,req.oracleMobile.storage.getById(registry.getIncidentPhotoStorageName(),  incident_id + '_03',  { sync: true }).then(
            function (result) {
                base64data = result.result;
                base64data = JSON.parse(base64data);
                dbResult.photo3 = base64data.image;
                res.status(result.statusCode).send(dbResult);                              
            },
            function (err) {             
                //ignore no photo and send db result
                res.status(200).send(dbResult);
            }
        ), function (result) {res.status(result.statusCode).send(dbResult)}
        ); 
    }
);

}


Solution

  • Now that I see you use promise-join and not Bluebirds Promise.join that makes your code almost trivial

    const join = require('promise-join');
    
    function getIncident(req, res) {
        getIncidentRow(req).then(dbResult => {
            var incident_id = dbResult.id;
            const getItem = suffix => req.oracleMobile.storage.getById(registry.getIncidentPhotoStorageName(), incident_id + suffix,  { sync: true })
                .then(result => {
                    let base64data = result.result;
                    base64data = JSON.parse(base64data);
                    return base64data.image;
                    // or just ... return JSON.parse(result.result).image;
                });
    
            return join({
                photo: getItem('_01'), 
                photo2: getItem('_02'), 
                photo3: getItem('_03')
            })
            .then(({result, errors}) => {
                Object.assign(dbResult, result);
                res.status(200).send(dbResult);
            });
        });
    }
    

    promise-join handles errors, therefore in .then({result, errors}) => { gets two objects

    result === {
        photo: /* resolved value of getItem('_01') */
        photo2: /* resolved value of getItem('_02') */
        photo3: /* resolved value of getItem('_03') */
    }
    

    and

    errors === {
        photo: /* rejected value of getItem('_01') */
        photo2: /* rejected value of getItem('_02') */
        photo3: /* rejected value of getItem('_03') */
    }
    

    i.e. if photo resolves, there will be a photo property in result, but not in errors, and so on for photo2 and photo3

    Now, since your original code simply ignored any photo rejections, this is perfect

    Object.assign(dbResult, result)
    

    Is all you need to assign any resolved photos to dbResult