gpt4 book ai didi

javascript - 使用 Node js 读取谷歌电子表格数据不起作用..?

转载 作者:行者123 更新时间:2023-12-02 22:04:47 24 4
gpt4 key购买 nike

我正在使用postman来调用api。我正在尝试使用 Node js 读取 google 电子表格行。响应正在控制台上打印,但没有返回到 postman 。

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 函数返回响应

最佳答案

res.send(response); 移至 try block 内并阅读 how to return response from an async call .

此外,return jsonObj 应该位于 try block 内

关于javascript - 使用 Node js 读取谷歌电子表格数据不起作用..?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59752433/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com