因此,基本上我想从单个Google SpreadSheet获取工作表列表(例如工作表1,工作表2),并将其显示在前端。 我已经启动并运行了一个Node.js服务器,并且可以从Google Sheets的后端获取数据。
我想做的就是在渲染时将数组发送到我的页面。目前,我可以在console.log中获取数据,但是页面无法呈现。
index.js //服务器
let myContainerView = UIView()
myContainerView.translatesAutoresizingMaskIntoConstraints = false
myContainerView.backgroundColor = .blue
// add container view to self view
view.addSubview(myContainerView)
// constrain container view center X and Y, width and height both 240-pts
NSLayoutConstraint.activate([
myContainerView.centerXAnchor.constraint(equalTo: view.centerXAnchor, constant: 0.0),
myContainerView.centerYAnchor.constraint(equalTo: view.centerYAnchor, constant: 0.0),
myContainerView.widthAnchor.constraint(equalToConstant: 240.0),
myContainerView.heightAnchor.constraint(equalToConstant: 240.0),
])
let myInnerView = UIView()
myInnerView.translatesAutoresizingMaskIntoConstraints = false
myInnerView.backgroundColor = .red
// add inner view to container view
myContainerView.addSubview(myInnerView)
// constrain inner view with 20-pts padding on all four sides
NSLayoutConstraint.activate([
// top and left are 20-pts from superview top and left
myInnerView.topAnchor.constraint(equalTo:
myContainerView.topAnchor, constant: 20.0),
myInnerView.leadingAnchor.constraint(equalTo:
myContainerView.leadingAnchor, constant: 20.0),
// bottom and right are *minus* 20-pts from superview bottom and right
myInnerView.bottomAnchor.constraint(equalTo:
myContainerView.bottomAnchor, constant: -20.0),
myInnerView.trailingAnchor.constraint(equalTo:
myContainerView.trailingAnchor, constant: -20.0),
])
myGenerator.js
const express = require('express');
const path = require('path');
const myGenerator = require('./app/myGenerator.js')
const app = express();
app.set('view engine', 'pug');
app.set('views', path.join(__dirname, 'views'));
app.use(express.static('./app'));
app.get('/home', async (req, res, next) => {
Promise.all([
myGenerator.GetAllSheets()
]).then((value) => {
res.render('home', { title: 'Hey', map1: value})
})
});
app.listen(3000, () => console.log('Example app listening on port 3000!'));
home.pug //视图
module.exports = {
GetAllSheets: function() {
return new Promise((resolve, reject) => {
const googleSheetID = "REMOVED";
// GOOGLE SHEETS CODE
// Load client secrets from a local file.
fs.readFile(__dirname + '/client_secret.json', (err, content) => {
if (err) return console.log('Error loading client secret file:', err);
// Authorize a client with credentials, then call the Google Sheets API.
GoogleFunctions.authorize(JSON.parse(content), getData);
});
function getData(auth) {
const sheets = google.sheets({version: 'v4', auth});
sheets.spreadsheets.get({
spreadsheetId: `${googleSheetID}`
}, (err, res) => {
if (err) return console.log('The API returned an error: ' + err);
const rows = res.data.sheets;
var map1 = rows.map(function(sheet) {
return sheet.properties.title
})
console.log(map1);
return map1;
});
}
})
}
}
如果有人能指出我正确的方向,将不胜感激。干杯
答案 0 :(得分:1)
在使用new Promise
到reject
或resolve
时要记住,否则您将得到一个永无止境的诺言。
下面是修正后的代码外观。
我已在更改的3个地方添加了评论。
另外,在路由内出现catch
错误也是个好主意,您可以返回500
错误,等等。
GetAllSheets: function() {
return new Promise((resolve, reject) => {
const googleSheetID = "REMOVED";
fs.readFile(__dirname + '/client_secret.json', (err, content) => {
//remember to also reject promise if error.
if (err) return reject('Error loading client secret file:', err);
GoogleFunctions.authorize(JSON.parse(content), getData);
});
function getData(auth) {
const sheets = google.sheets({version: 'v4', auth});
sheets.spreadsheets.get({
spreadsheetId: `${googleSheetID}`
}, (err, res) => {
//again if we have error, reject
if (err) return reject('The API returned an error: ' + err);
const rows = res.data.sheets;
var map1 = rows.map(function(sheet) {
return sheet.properties.title
})
//lets return our result
return resolve(map1);
});
}
})
}