accessing saved data with mongodb in an apofy actor

ive tried saving the data to a rawdata.json file from the data i scrape from my actors, however i dont get a json output even thought the scraping works how would i save the data to the apify console that i can then use mongodb to take that data and put it in my database - i have my mongodb schema already setup so how would i save the data to the apify console and access it heres what i have for saving the json file so far:
4 Replies
plain-purple
plain-purpleOP•2y ago
bambawRouter.addHandler('BAMBAW_PRODUCT', async ({ page, request }) => {
try {
console.log('Scraping products');

const site = 'Bambaw';

const title = await page.$eval('h1.product__title', (el) => el.textContent?.trim() || '');

const descriptions = await ......

const productData = {
url: request.loadedUrl,
site,
title,
descriptions,
originalPrice,
salePrice,
shippingInfo,
reviewScore,
reviewNumber,
};

productList.push(productData);

console.log('Scraped ', productList.length, ' products')
// Read the existing data from the rawData.json file
let rawData: any = {};
try {
const rawDataStr = fs.readFileSync('rawData.json', 'utf8');
rawData = JSON.parse(rawDataStr);
} catch (error) {
console.log('Error reading rawData.json:', error);
}

// Append the new data to the existing data
if (rawData.productList) {
rawData.productList.push(productData);
} else {
rawData.productList = [productData];
}

// Write the updated data back to the rawData.json file
fs.writeFileSync('rawData.json', JSON.stringify(rawData, null, 2));
console.log('rawData.json updated for Bambaw');
} catch (error) {
console.log('Error scraping product:', error);
bambawQueue.reclaimRequest(request);
return;
}
});
bambawRouter.addHandler('BAMBAW_PRODUCT', async ({ page, request }) => {
try {
console.log('Scraping products');

const site = 'Bambaw';

const title = await page.$eval('h1.product__title', (el) => el.textContent?.trim() || '');

const descriptions = await ......

const productData = {
url: request.loadedUrl,
site,
title,
descriptions,
originalPrice,
salePrice,
shippingInfo,
reviewScore,
reviewNumber,
};

productList.push(productData);

console.log('Scraped ', productList.length, ' products')
// Read the existing data from the rawData.json file
let rawData: any = {};
try {
const rawDataStr = fs.readFileSync('rawData.json', 'utf8');
rawData = JSON.parse(rawDataStr);
} catch (error) {
console.log('Error reading rawData.json:', error);
}

// Append the new data to the existing data
if (rawData.productList) {
rawData.productList.push(productData);
} else {
rawData.productList = [productData];
}

// Write the updated data back to the rawData.json file
fs.writeFileSync('rawData.json', JSON.stringify(rawData, null, 2));
console.log('rawData.json updated for Bambaw');
} catch (error) {
console.log('Error scraping product:', error);
bambawQueue.reclaimRequest(request);
return;
}
});
sunny-green
sunny-green•2y ago
@harish you can add a language to the three backticks for your code block to give it syntax highlighting–which will help you get help 🙂
plain-purple
plain-purpleOP•2y ago
i need to be able to save data to the apify console that i can then access from the actor, clean the data, and save to a database like mongodb
sunny-green
sunny-green•2y ago
@harish if you can get me access to the actor’s source or figure out syntax highlighting here, I can take a look and try to help If you still need it 🙂

Did you find this page helpful?