【发布时间】:2020-01-15 13:28:52
【问题描述】:
我正在使用邮递员调用 api。我正在尝试使用节点 js 读取谷歌电子表格行。响应正在控制台上打印,但没有返回给邮递员。
index.js 文件
app.post('/myapi/getClientkey',async (req, res) => {
var response = null;
console.log("Inside myapi");
try {
response = await spreadsheet.getRecord();
} catch(err){
}
res.send(response);
});
电子表格.js
var config = require('./config.json');
var GoogleSpreadsheet = require('google-spreadsheet');
var creds = {
client_email: config.client_email,
private_key: config.private_key
}
var doc = new GoogleSpreadsheet(config.GOOGLE_SHEET_ID)
var sheet = null;
exports.getRecord = async function () {
console.log('Inside - getRecord');
var name = null;
var jsonObj = {};
try {
doc.useServiceAccountAuth(creds, async function (err) {
// Get all of the rows from the spreadsheet.
await doc.getRows(1, async function (err, rows) {
if(rows != null){
var oneRow = rows[0];
name = oneRow.name;
console.log("name :"+name);
jsonObj.client_name = name.toString();
}
console.log(jsonObj);
});
});
}catch(err){
console.log("err :"+err);
}
return jsonObj;
};
如何等待 getRecord 函数返回响应
【问题讨论】:
-
检查线程Google Drive API promise doesn't return at all when called from an AWS Lambda function (but works fine locally) 上的下一个答案,它的请求基本相同。让我知道它是否对您有帮助。
标签: javascript node.js google-sheets-api