解析承诺和解析相互关系-Javascript API

解析承诺和解析相互关系-Javascript API,javascript,parse-platform,promise,Javascript,Parse Platform,Promise,好的,伙计们!有人能帮我吗? 我有一个解析数据库,如下所示: 人名 Productid,序列号 发票ID、产品、个人ID 我需要构建一个函数,返回一个对象数组, 在每个对象中,person.name、invoice.id、invoice.products 我的英语不是很好,所以我希望你明白我的意思 所以我使用Parse.Promise查询人员和发票 function Obj(name, invoiceId, products) { return { name: name

好的,伙计们!有人能帮我吗? 我有一个解析数据库,如下所示:

人名 Productid,序列号 发票ID、产品、个人ID

我需要构建一个函数,返回一个对象数组, 在每个对象中,person.name、invoice.id、invoice.products 我的英语不是很好,所以我希望你明白我的意思

所以我使用Parse.Promise查询人员和发票

function Obj(name, invoiceId, products) {
    return {
        name: name || '',
        invoiceId: invoiceId || '',
        products: products || []
    };
}
function retreiveData(aName) {
    var retval = [],
            personQuery = new Parse.Query("Person");
    personQuery.equalTo('name', aName).first().then(function(person) {
        var invoiceQuery = new Parse.Query("Invoice");
        return invoiceQuery.equalTo('personId', person.id).query().find();
    }), then(function(invoices) {
        //until here everythinks work fine
        // now i've the invoices list and i have to look for related products
        var promise = new Parse.Promise();
        _.each(invoices, function(invoice) {
            var obj = new Obj(aName, invoice.id),
                    promise = new Parse.Promise();
            invoice.relation('products').query().find().then(function(products) {
                obj.products = products;
                // here i expect to have the obj fulfilled with all data
                retval.push(obj);
                promise.resolve();
            });

            return promise;
        });

        return promise;
    });
}

var aName = 'Paul';
retreiveData(aName).then(function(retval) {
    console.log('success');
    /* here i have to have somethin like 
        retval=[{
            name='paul',
            invoiceId='12412412',
            products=[{prod1},{prod2},{prod3}]
        },{
            name='paul',
            invoiceId='67413412',
            products=[{prod5},{prod10},{prod33}]
        }]
       */
});
有什么想法吗? 我知道内心的承诺是错误的,但我不知道如何去弥补
谢谢你,伙计

谢谢,我查过了!但问题是,我必须为每个发票创建一个承诺,并将每个产品的承诺嵌套到发票中…我确定有其他方法来完成这项工作…还是没有?基本上,我不知道每个查询将返回多少行,以及需要实例化多少承诺……我非常困惑!当获取任意大小的数组时,您可以在调用时嵌套。好的,我将尝试!谢谢我会让你知道的!