我目前正在设置一个快递服务器,该服务器从我抓取的网页中接收数据并显示结果。我无法获取此obj /数组进行渲染。我可以制作一个完全不同的数组渲染。
我目前正在使用path,express,hbs,axios和cheerio。我有一个
{{data}}
的hbs文件,在我的app.js上,我有一个
app.get('', (req, res) => {
res.render('index', {
title: 'ClearVision',
data: newFunction(),
name: 'Chris'
})
})
这是下面的代码。
const getdeals = (callback) => {
axios(url, {
headers: {
Cookie: "xx=x;"
}
})
.then(response => {
const html = response.data;
const $ = cheerio.load(html);
// Deals Page
const statsTable = $('tbody > tr');
const totaldeals = [];
// Loop Table for data in each row
statsTable.each(function () {
const nwline = "\n"
let date = $(this).find('td:nth-child(1)').text()
let bodydeals = $(this).find('td:nth-child(2)').text()
let newdeal = $(this).find('td:nth-child(3)').text()
let revdeal = $(this).find('td:nth-child(4)').text()
let monthlydealrev = $(this).find('td:nth-child(5)').text()
// Clear /n
if (date.includes(nwline)) {
date = date.toString().replace("\n", ""),
date = date.toString().replace("\n", "")
}
// Clear /n
if (bodydeals.includes(nwline)) {
bodydeals = bodydeals.toString().replace("\n", ""),
bodydeals = bodydeals.toString().replace("\n", ""),
bodydeals = bodydeals.toString().replace("\n", "")
}
// Clear /n
if (newdeal.includes(nwline)) {
newdeal = newdeal.toString().replace("\n", ""),
newdeal = newdeal.toString().replace("\n", ""),
newdeal = newdeal.toString().replace("\n", "")
}
// Clear /n
if (revdeal.includes(nwline)) {
revdeal = revdeal.toString().replace("\n", ""),
revdeal = revdeal.toString().replace("\n", ""),
revdeal = revdeal.toString().replace("\n", "")
}
// Clear /n (lookup jquery table functions)
if (monthlydealrev.includes(nwline)) {
monthlydealrev = monthlydealrev.toString().replace("\n", ""),
monthlydealrev = monthlydealrev.toString().replace("\n", ""),
monthlydealrev = monthlydealrev.toString().replace("\n", "")
}
totaldeals.push({
date,
bodydeals,
newdeal,
revdeal,
monthlydealrev
})
})
callback({
totaldeals
})
//console.log(totaldeals[1].date)
})
.catch(console.error);
}
function newFunction() {
//return [getdeals(result => console.log(result.totaldeals[0].newdeal))];
return [getdeals(result => console.log(result.totaldeals))]
}
在右下角,我可以刷新页面,并打印出result.totaldeals(result是一个obj,里面有totaldeals 一个数组) 如果我删除console.log并尝试将其返回的数据不打印到console.log(显然是我删除了它),但是我无法将数据渲染到屏幕上。如果我创建一个类似var x = [''one','two']的数组,则可以很好地将其显示在视图上。