2016-01-31 72 views
1

我遇到了一些奇怪的問題。將對象添加到parse.com查詢中的陣列

我需要添加對象到一個數組,並使用正常的array.push(object)似乎沒有工作(沒有任何被推送)。

在parse.com查詢中推動內部和外部for循環。

我試過一些調試,並在所有步驟console.log返回結果,如我所料。

我的問題是:有什麼我不知道有關parse.com查詢如何與array.push連接,或者for-​​loops?任何幫助,將不勝感激。

詳細信息是實體(數組)和實體(對象)。

我期望的結果是這樣的:

entities = [ 
    { url: '/first', changefreq: 'weekly', priority: 1.0 }, 
    { url: '/second', changefreq: 'monthly', priority: 0.9 }, 
    { url: '/third', changefreq: 'weekly', priority: 0.5 } 
]; 

但我只得到了第一個推到出現在數組中。

我檢查了所有的parse.com查詢,他們確實返回我需要的數據並在下面的例子中使用。

我的代碼:

module.exports = { 
 
    getSitemap: function(callback) { 
 
    "use strict"; 
 
    var Page = Parse.Object.extend('Page'), 
 
     pageQuery = new Parse.Query(Page), 
 
     Article = Parse.Object.extend('Article'), 
 
     articleQuery = new Parse.Query(Article), 
 
     Profile = Parse.Object.extend('Profile'), 
 
     profileQuery = new Parse.Query(Profile), 
 
     Category = Parse.Object.extend('Category'), 
 
     categoryQuery = new Parse.Query(Category), 
 
     entities = [], 
 
     entity = {}, 
 
     i, 
 
     sitemap; 
 

 
    entity.url = '/'; 
 
    entity.changefreq = 'weekly'; 
 
    entity.priority = 1.0; 
 
    entities.push(entity); 
 
    entity = {}; 
 

 
    articleQuery.equalTo('published', true); 
 
    articleQuery.select("permalink"); 
 
    articleQuery.find().then(function(results) { 
 
     for (i = 0; i < results.length; i += 1) { 
 
     entity.url = '/article/' + results[i].get('permalink'); 
 
     entity.changefreq = 'monthly'; 
 
     entity.priority = 0.9; 
 
     entities.push(entity); 
 
     entity = {}; 
 
     } 
 
    }, function(error) { 
 
     // do nothing 
 
    }); 
 

 
    pageQuery.select("pagePermaLink"); 
 
    pageQuery.find().then(function(results) { 
 
     for (i = 0; i < results.length; i += 1) { 
 
     entity.url = '/page/' + results[i].get('pagePermaLink'); 
 
     entity.changefreq = 'monthly'; 
 
     entity.priority = 0.7; 
 
     entities.push(entity); 
 
     entity = {}; 
 
     } 
 
    }, function(error) { 
 
     // do nothing 
 
    }); 
 

 
    profileQuery.select("objectId"); 
 
    profileQuery.find().then(function(results) { 
 
     for (i = 0; i < results.length; i += 1) { 
 
     entity.url = '/author/' + results[i].id; 
 
     entity.changefreq = 'monthly'; 
 
     entity.priority = 0.6; 
 
     entities.push(entity); 
 
     entity = {}; 
 
     } 
 
    }, function(error) { 
 
     // do nothing 
 
    }); 
 

 
    categoryQuery.select("categoryPermaLink"); 
 
    categoryQuery.find().then(function(results) { 
 
     for (i = 0; i < results.length; i += 1) { 
 
     entity.url = '/category/' + results[i].get('categoryPermaLink'); 
 
     entity.changefreq = 'weekly'; 
 
     entity.priority = 0.5; 
 
     entities.push(entity); 
 
     entity = {}; 
 
     } 
 
    }, function(error) { 
 
     // do nothing 
 
    }); 
 

 
    sitemap = sm.createSitemap({ 
 
     hostname: 'http://brianemilius.com', 
 
     cacheTime: 300000, 
 
     urls: entities 
 
    }); 
 
    sitemap.toXML(function(err, xml) { 
 
     if (err) { 
 
     callback({ 
 
      error: err 
 
     }); 
 
     } 
 
     callback(xml); 
 
    }); 
 
    } 
 
};

+0

這可能有助於瞭解[JavaScript的承諾(http://blog.parse.com/learn/engineering/whats-so-great-about-javascript-promises/) – Skam

+0

我已經做了和發現什麼都不能解釋我的問題。也許你看到我不喜歡的東西? –

+0

請console.log(結果)。在回調中。可能是你沒有得到數據 –

回答

2

的代碼開始幾個非同步操作,然後立即採取依靠他們的結果的行動。我們需要修改代碼以等待每個操作完成。

// ... 
// above here from the OP 

articleQuery.equalTo('published', true); 
articleQuery.select("permalink"); 
// hang on to the promise 
var articlePromise = articleQuery.find().then(function(results) { 
    for (i = 0; i < results.length; i += 1) { 
    entity.url = '/article/' + results[i].get('permalink'); 
    entity.changefreq = 'monthly'; 
    entity.priority = 0.9; 
    entities.push(entity); 
    entity = {}; 
    } 
}, function(error) { 
    // do nothing 
}); 

pageQuery.select("pagePermaLink"); 
// hang on to the promise 
var pagePromise = pageQuery.find().then(function(results) { 
    for (i = 0; i < results.length; i += 1) { 
    entity.url = '/page/' + results[i].get('pagePermaLink'); 
    entity.changefreq = 'monthly'; 
    entity.priority = 0.7; 
    entities.push(entity); 
    entity = {}; 
    } 
}, function(error) { 
    // do nothing 
}); 

profileQuery.select("objectId"); 
// hang on to the promise 
var profilePromise = profileQuery.find().then(function(results) { 
    for (i = 0; i < results.length; i += 1) { 
    entity.url = '/author/' + results[i].id; 
    entity.changefreq = 'monthly'; 
    entity.priority = 0.6; 
    entities.push(entity); 
    entity = {}; 
    } 
}, function(error) { 
    // do nothing 
}); 

categoryQuery.select("categoryPermaLink"); 
// hang on to the promise 
var categoryPromise = categoryQuery.find().then(function(results) { 
    for (i = 0; i < results.length; i += 1) { 
    entity.url = '/category/' + results[i].get('categoryPermaLink'); 
    entity.changefreq = 'weekly'; 
    entity.priority = 0.5; 
    entities.push(entity); 
    entity = {}; 
    } 
}, function(error) { 
    // do nothing 
}); 

// only after all of the promises that create entities are fulfilled 
// do we execute the logic that relies on that data 

var promises = [articlePromise, pagePromise, profilePromise, categoryPromise]; 
Parse.Promise.when(promises).then(function() { 
    sitemap = sm.createSitemap({ 
     hostname: 'http://brianemilius.com', 
     cacheTime: 300000, 
     urls: entities 
    }); 
    sitemap.toXML(function(err, xml) { 
     if (err) { 
     callback({ 
      error: err 
     }); 
     } 
     callback(xml); 
    }); 
}, function(error) { 
    // handle error 
}); 
+0

這正是我想要的。謝謝一堆! –