我正在尝试使用 Express 渲染 Google 电子表格中的数据,并将要渲染的单元格范围放入其中并循环后,出现错误:“错误 [ERR_HTTP_HEADERS_SENT]:在将标题发送到客户端后无法设置标题”。
该电子表格有 3 行和 6 列,我不确定我做错了什么,因为错误是持续存在的,无论如何我都无法摆脱它。
我的代码:
const express = require('express');
const router = express.Router();
const {google} = require('googleapis');
const keys = require('../credentials.json');
const {
} = require('googleapis');
const keys = require('../credentials.json');
/* GET portfolio page. */
router.get('/', function (req, res, next) {
const client = new google.auth.JWT(
keys.client_email,
null,
keys.private_key,
['https://www.googleapis.com/auth/spreadsheets.readonly']
);
client.authorize(function (err) {
if (err) {
console.log(err);
return;
} else {
console.log('Connected');
gsrun(client);
}
});
async function gsrun(cl) {
const gsapi = google.sheets({
version: 'v4',
auth: cl
});
const optPort1 = {
spreadsheetId: '1W1OKGmGU6Io-1FhWjZLyPkGZz9Ky829zurAzcwmXiHg',
range: ['Portfolio Page!A4:F6']
};
let spreadvals1 = await gsapi.spreadsheets.values.get(optPort1);
console.log(spreadvals1.data.values);
const cols1 = spreadvals1.data.values || [];
const colsdata = cols1.map((element) => {
res.set('Content-Type', 'text/html');
res.render('portfolio', {
headlinePortfolio: element[0],
subheadlinePortfolio: element[1],
image1: element[3],
client: element[4],
campaign: element[5]
})
});
}
});
module.exports = router;
我的 HTML 看起来像这样:
<div class="page-header">
<div class="text-headline">
<div class="salutation">{{headlinePortfolio}}</div>
</div>
<div class="text-subheadline">
<div class="descr">{{subheadlinePortfolio}}</div>
</div>
慕雪6442864
相关分类