I have a function which creates objects from scraped website data. The objects look like this:
{
name: 'Object Name',
baseURL: 'http://xyz123.com',
mainImg_select: '#item_image a',
mainImg_ref: 'href',
moreImg_select: '.extra_images',
moreImg_ref: 'href',
one_brand: '',
urls: [],
categories:
[ { name: 'Cat 1',
url: 'http://xyz123.com/category1' },
{ name: 'Cat 2',
url: 'http://xyz123.com/category2' },
{ name: 'Cat 3',
url: 'http://xyz123.com/category3' }
]
}
I'm running it through some functions that use the request
and cheerio
modules. The request
function is, somehow, deleting my objects' categories
property; when logging the object I see "categories: undefined"
(so the property is still there, just its contents are removed.
What's REALLY bizarre is that this ONLY happens when I'm using an object stored in a variable that is created by my script. If I create the object, log it, then copy and paste it directly into the code (like above except var object123 = {stuff goes here}
) then my Node script works just fine.
PrepJSON
is a class which takes a JSON file turns it into an object. To avoid this becoming a massive wall of code, I've pastebin'd it here: http://pastebin.com/Tgx0nnmR
The only difference is that in one I'm using a returned object from my prepJSON
function, in the other I'm console.logging that object, running the function, copying the log, putting it in a variable - then magically the script works.
The rest of the object is NOT touched by the script - only the categories, which stores an array of objects. For completeness, here's how the object.categories
are created in prepJSON. This is the only time a script touches them:
storeCats.categories = (function(){
$('.view-Departments .field-content a').each(function(index){
storeCats.categories.push({'name': $(this).text(), 'url': $(this).attr('href')});
if(index == $('.view-Departments .field-content a').length -1) {callback(null);}
})}
)();