Home > Mobile >  Problem accessing an array from within a function
Problem accessing an array from within a function

Time:02-03

I am creating a app that will scrape Indeed and then will send me an email of the array. I have scrapped the postings successfully and can send emails; but when I try to send the email all that I get is [object, Object] in my email.

const PORT =8000;
const axios = require('axios');
const cheerio = require('cheerio');
const express = require('express');
const nodemailer = require('nodemailer');

const app = express()
const url = 'https://www.indeed.com/jobs?q=Junior engineer&l=Remote&fromage=1';

//Axios will return a promise and once the promise is returned we will get the response(data)//

axios(url)
    .then(response => {
        const html = response.data
        const $ = cheerio.load(html)
        const indeedJobPostings =[]

        //After cheerio has loaded our html, we begin by searching through a .resultContent class for our needed title and company //
        $('.resultContent',html).each(function(){
             title = $(this).find('.jobTitle').text()
             company = $(this).find('.companyName').text()
            indeedJobPostings.push({
                title, company
            })
            
        })
        ////////////
        let transporter = nodemailer.createTransport({
            host: 'smtp.gmail.com',
            port: 587,
            secure: false,
            requireTLS: true,
            auth: {
                user: 'username',
                pass: 'password'
            }
        });

        let mailOptions = {
            from: 'email',
            to: 'email',
            subject: 'Test',
            html: `<h2>Here are your daily job postings from Indeed:</h2> <ul>${indeedJobPostings}</ul>`
        };

        console.log('post log',indeedJobPostings, mailOptions)

        transporter.sendMail(mailOptions, (error, info) => {
            if (error) {
                return console.log(error.message);
            }
            console.log('success');
        });

        ///////////
    }).catch(err => console.log(err))


app.listen(PORT, () => console.log(`Server running on PORT ${PORT}`))


When I console.log(indeedJobPosting, mailOptions) indeedJobPostings prints as suspected but mailOptions sends this:

{
  from: 'email',
  to: 'email',
  subject: 'Test',
  html: '<h2>Here are your daily job postings from Indeed:</h2> <ul>[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]</ul>'
}

CodePudding user response:

indeedJobPostings.push({
    title, company
})

populates the indeedJobPostings array with objects which have title and company properties. Converting this to a string in the template expression

${indeedJobPostings}

create a comma separated list of "[object Object]" strings which result from Object.prototype.toString converting the objects to strings, which are then listed by Array.prototype.toString using comma separators in its own toString operation.

You could either explicitly convert indeedJobPostings object entries to text when creating the email body, or perhaps push text in the first place:

indeedJobPostings.push(`Title: ${title}\nCompany: ${company}\n\n`);

(for example).

  •  Tags:  
  • Related