如何使用 promise join 从存储中检索 3 条记录并将它们放入 dbResult

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

如何在检索3条记录并放入dbResult时使用promise 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) {
               dbResult.photo = imageData.uploadPhotoIcon;
                //ignore no photo and send db result
                res.status(200).send(dbResult);
            }
        );

我想检索3条记录并像dbResult.photo2 = base64data.image一样添加它;使用承诺加入。 我试过“returnpromise.join(retrieve1,retrieve2,retrieve3);”。它不起作用。 这是我的代码...

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)}
        ); 
    }
);

}

现在我看到你使用 promise-join 而不是 Bluebirds Promise.join 这使得你的代码几乎微不足道

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 处理错误,因此在 .then({result, errors}) => { 中得到两个对象

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

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

即如果 photo 解决了,结果会有 photo 属性,但不会出现错误,photo2 和 photo3

依此类推

现在,由于您的原始代码只是简单地忽略了任何照片拒绝,这就完美了

Object.assign(dbResult, result)

您只需要将任何已解析的照片分配给 dbResult