I'm new to both node.js
and Promise
functionality so please forgive me if this question is really foolish.
I'm trying to make a child Promise
call forEach
child within a Parent call (if that makes sense).
This is my code:
return new Promise(function(resolve, reject) {
var authorMapArray = new Array
db.sequelize.query(authorQuery, {
replacements: queryParams
}).spread(function(authorSitemap) {
authorSitemap.forEach(function(obj) {
/*
return new Promise (function(resolve, reject){
var thisQuery = photoQuery + ' AND author = ' + obj.id.toString();
db.sequelize.query(thisQuery, {
queryParams
}).spread(function(authorImages) {
var authorImageArray = new Array;
authorImages.forEach(function(obj) {
var imgLink = { url: imgHost + obj.img_id + '.jpg', title : img_tags }
authorImageArray.push(imgLink);
})
});
resolve(authorImageArray);
});
*/
var authorLink = { url: 'author/' + obj.id, /*img: authorImageArray,*/ changefreq: 'weekly', priority: 0.6, lastmodrealtime: true }
siteMapArray.push(authorLink);
});
resolve(siteMapArray);
//and finally create it
createSiteMap(siteMapArray);
});
})
You'll note, the section in the middle is commented out. When I run the code like this I get the results I expect, that is the authorLink added to the sitemap. When I uncomment the code (in order to include the images associated with the author in the sitemap), not even the authorlinks are added.
How do I get the images for the author included within their record?
EDIT
This is the more complete code:
function createSiteMap(myURLs) {
var rows = 10000;
var totalMaps = Math.trunc(myURLs.length/rows)+1;
var today = new Date();
var mySitemaps = new Array;
for (var i=1; i<totalMaps+1; i++) {
var filename = "public/sitemap-" + i.toString() + ".xml";
var sitemap = sm.createSitemap({
hostname: hostname,
cacheTime: 600000, //600 sec (10 min) cache purge period
urls: myURLs.slice((i-1)*rows,i*rows)
});
fs.writeFileSync(filename, sitemap.toString());
mySitemaps.push(filename);
}
// this needs to create sitemap tags not url tags
var smi = sm.buildSitemapIndex({
urls: mySitemaps
});
fs.writeFileSync("public/sitemap.xml", smi.toString());
process.exit();
}
function uniq(a) {
var seen = {};
return a.filter(function(item) {
return seen.hasOwnProperty(item) ? false : (seen[item] = true);
});
}
function getPhotos() {
return new Promise(function(resolve, reject) {
var siteMapArray = new Array()
var tags = new Array()
siteMapArray.push ({ url: '/' , changefreq: 'weekly', priority: 0.8, lastmodrealtime: true, lastmodfile: 'views/home.hbs' },)
db.sequelize.query(photoQuery, {
replacements: queryParams
}).spread(function(makeSiteMap) {
makeSiteMap.forEach(function(obj) {
// images for sitemap
var img_tags = obj.tags.replace(/,/g , " ");
var imgLink = { url: imgHost + obj.img_id + '.jpg', title : img_tags }
var siteLink = { url: 'photo/' + obj.img_id, img: imgLink, changefreq: 'weekly', priority: 0.6, lastmodrealtime: true }
siteMapArray.push(siteLink);
obj.tags = obj.tags.split(',').map(function(e) {
return e.trim().split(' ').join('+');
});
for (var tag in obj.tags) {
tags.push(obj.tags[tag])
}
});
resolve (siteMapArray);
//tags for sitemap
var uniqueTags = uniq(tags);
for (var tag in uniqueTags) {
var siteLink = { url: '/search/' + uniqueTags[tag], changefreq: 'weekly', priority: 0.8, lastmodrealtime: true }
siteMapArray.push (siteLink);
}
//now author tags
return new Promise(function(resolve, reject) {
var authorMapArray = new Array
db.sequelize.query(authorQuery, {
replacements: queryParams
}).spread(function(authorSitemap) {
authorSitemap.forEach(function(obj) {
/*
return new Promise (function(resolve, reject){
var thisQuery = photoQuery + ' AND author = ' + obj.id.toString();
db.sequelize.query(thisQuery, {
queryParams
}).spread(function(authorImages) {
var authorImageArray = new Array;
authorImages.forEach(function(obj) {
var imgLink = { url: imgHost + obj.img_id + '.jpg', title : img_tags }
authorImageArray.push(imgLink);
})
});
resolve(authorImageArray);
});
*/
var authorLink = { url: 'author/' + obj.id, /*img: authorImageArray,*/ changefreq: 'weekly', priority: 0.6, lastmodrealtime: true }
siteMapArray.push(authorLink);
});
resolve(siteMapArray);
//and finally create it
createSiteMap(siteMapArray);
});
})
});
});
};
getPhotos();