从函数中访问数组时出现问题
Problem accessing an array from within a function
我正在创建一个应用程序,它会抓取 Indeed,然后会向我发送数组的电子邮件。我已经成功废贴,可以发邮件了;但是当我尝试发送电子邮件时,我收到的只是电子邮件中的 [object, Object]。
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}`))
当我 console.log(indeedJobPosting, mailOptions) indeedJobPostings 打印为可疑但 mailOptions 发送此:
{
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>'
}
indeedJobPostings.push({
title, company
})
用具有 title
和 company
属性的对象填充 indeedJobPostings
数组。将其转换为模板表达式中的字符串
${indeedJobPostings}
创建一个逗号分隔的“[object Object]”字符串列表,这些字符串由 Object.prototype.toString
将对象转换为字符串,然后由 Array.prototype.toString
使用逗号分隔符在其自身 toString
操作。
您可以在创建电子邮件正文时将 indeedJobPostings
对象条目显式转换为文本,或者首先推送文本:
indeedJobPostings.push(`Title: ${title}\nCompany: ${company}\n\n`);
(例如)。
我正在创建一个应用程序,它会抓取 Indeed,然后会向我发送数组的电子邮件。我已经成功废贴,可以发邮件了;但是当我尝试发送电子邮件时,我收到的只是电子邮件中的 [object, Object]。
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}`))
当我 console.log(indeedJobPosting, mailOptions) indeedJobPostings 打印为可疑但 mailOptions 发送此:
{
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>'
}
indeedJobPostings.push({
title, company
})
用具有 title
和 company
属性的对象填充 indeedJobPostings
数组。将其转换为模板表达式中的字符串
${indeedJobPostings}
创建一个逗号分隔的“[object Object]”字符串列表,这些字符串由 Object.prototype.toString
将对象转换为字符串,然后由 Array.prototype.toString
使用逗号分隔符在其自身 toString
操作。
您可以在创建电子邮件正文时将 indeedJobPostings
对象条目显式转换为文本,或者首先推送文本:
indeedJobPostings.push(`Title: ${title}\nCompany: ${company}\n\n`);
(例如)。