【问题标题】:How to Retrieve 3 records from storages and put them into dbResult using promise join如何从存储中检索 3 条记录并使用 promise join 将它们放入 dbResult
【发布时间】:2025-12-11 12:35:01
【问题描述】:

检索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;使用承诺加入。 我试过“return promise.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" - 如果这是蓝鸟承诺的事情,也许添加相关标签以便蓝鸟人可以提供帮助:p
  • @JaromandaX ,当我使用 return promise.join (retrieve1,retrieve2,retrieve3); 时它不起作用可能,我的代码结构是错误的。 :(
  • 好吧,Promise.join 记录在 here 并且要求最后一个参数是 handler function - 不知道为什么 Promise 库会诉诸回调,但是,我还没有阅读 Promise.join 的用例
  • @JaromandaX 我得到了数据。但是,照片出来 1 或 2,有时,它会完全出来 3 张照片。看来诺言迫不及待地加载了。仍在努力解决这个问题。 :(
  • 你能把代码贴在你使用 .join ... 的地方吗,并显示retrieve1 2 和 3 实际上是什么。将代码放在问题中而不是评论中

标签: javascript node.js npm promise bluebird


【解决方案1】:

现在我看到您使用 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

【讨论】:

  • 我可以知道我应该在哪里转换 base64 吗?
  • @Phyow - 立即尝试
  • 天哪!你帮了我很多。非常感谢您的帮助。我也会帮助别人。非常感谢。 _/_
  • 如果我想在 search_item not found 时返回默认图像的错误,在哪里添加?谢谢。
  • 在getItem函数中的一个catch中
最近更新 更多