I have a fairly simple javascript/jquery obeject being built by a Node.js scraper. Everything works as it should (obj.prod_name_select and its counterparts return jquery selectors and they're parsing just fine in Node).
Here's the object:
var product = {
name : $(obj.prod_name_select).text(),
systemName : function(){
return product.name.replace(/\s+/g, ' ');
},
gender : obj.gender,
infoLink : link,
designer : $(obj.label_name_select).first().text(),
description : function(){
var text = $(obj.description).each(function() {
var content = $(this).contents();
$(this).replaceWith(content);
});
return text;
},
price : $(obj.price_select).first().text(),
store : obj.store,
category : obj.general_cat,
spec_cat : obj.spec_cat
}
console.log(product);
When I run this with Node, it's all working fine except for the properties being set by functions. These return [FUNCTION] in node. Here's the log from console:
{ name: 'BAGGY PANTS!T',
systemName: [Function],
gender: 'men',
infoLink: 'http://www.hereisthecorrecturl.com',
designer: 'Fancy Designer',
description: [Function],
price: '$180.00',
store: 'Correct Store Name',
category: 'Correct Category',
spec_cat: 'Correct Category' }
Is this an asynchronous problem? It seems like the console.log is happening before the property functions are being run. What's the best way to solve this?
Instead of just assigning the function, you have to immediately execute it:
description : (function(){
return $(obj.description).each(function() {
var content = $(this).contents();
$(this).replaceWith(content);
});
}()),