解析Promise和Parse Relation toghether - Javascript API

时间:2014-11-15 20:26:45

标签: javascript parse-platform promise

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

Person(id, name)
Product(id,serial)
Invoice(id,<Relation>products, personId)

我需要构建一个返回Object数组的函数, 在每个Object person.name,invoice.id,invoice.products中 我对英语不太满意,所以我希望你理解我的意思。

所以我使用Parse.Promise查询&#34; person&#34;和#34;发票&#34;

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}]
        }]
       */
});

任何想法? 我知道内心的承诺是错误的,但我不明白如何解决 谢谢你们!

0 个答案:

没有答案