gpt4 book ai didi

javascript - 将对象添加到 parse.com 查询中的数组

转载 作者:行者123 更新时间:2023-11-30 16:18:28 25 4
gpt4 key购买 nike

我遇到了一个奇怪的问题。

我需要将对象添加到数组,使用普通的 array.push(object) 似乎不起作用(没有推送任何内容)。

推送发生在 parse.com 查询中的 for 循环内部和外部。

我已经尝试了一些调试,所有步骤的 console.log 都返回了我预期的结果。

我的问题是:关于 parse.com 查询如何与 array.push 或 for 循环相关联,是否有什么我不知道的?任何帮助将不胜感激。

细节是实体(数组)和实体(对象)。

我期望这样的结果:

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

最佳答案

代码启动了几个异步操作,然后立即采取依赖于它们的结果的操作。我们需要修改代码等待每个操作完成。

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

关于javascript - 将对象添加到 parse.com 查询中的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35116949/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com