Node.js 无法读取属性';已安装';未定义的express js
我还没有在Express js或google sheets api中找到这个问题的示例。我正在运行一个express应用程序,该应用程序正在接收POST req,然后我将其传递给一个使用oAuth2的函数,然后将数据作为附加行发送到google工作表 调用updateSheet()时,会出现以下错误Node.js 无法读取属性';已安装';未定义的express js,node.js,express,google-sheets,Node.js,Express,Google Sheets,我还没有在Express js或google sheets api中找到这个问题的示例。我正在运行一个express应用程序,该应用程序正在接收POST req,然后我将其传递给一个使用oAuth2的函数,然后将数据作为附加行发送到google工作表 调用updateSheet()时,会出现以下错误 UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'installed' of undefined at r
UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'installed' of undefined
at resolve (C:\Users\luther\Desktop\Pantree\index.js:50:69)
at new Promise (<anonymous>)
at authorize (C:\Users\luther\Desktop\Pantree\index.js:49:10)
at updateSheet (C:\Users\luther\Desktop\Pantree\index.js:91:28)
at app.post (C:\Users\luther\Desktop\Pantree\index.js:161:5)
at Layer.handle [as handle_request] (C:\Users\luther\Desktop\Pantree\node_modules\express\lib\router\layer.js:95:5)
at next (C:\Users\luther\Desktop\Pantree\node_modules\express\lib\router\route.js:137:13)
at Route.dispatch (C:\Users\luther\Desktop\Pantree\node_modules\express\lib\router\route.js:112:3)
at Layer.handle [as handle_request] (C:\Users\luther\Desktop\Pantree\node_modules\express\lib\router\layer.js:95:5)
at C:\Users\luther\Desktop\Pantree\node_modules\express\lib\router\index.js:281:22
我的app.post
app.post('/api/updateSheet', (req,res)=>{
updateSheet()
});
我忘记在updateSheets()调用中向authorize调用传递creds
“TypeError:无法读取解析时未定义的属性'installed'(C:\Users\luther\Desktop\Pantree\index.js:50:69)”那么第50行的
index.js中有什么内容?(和49,基本上是它周围的一切=)@mike pomax kamermans我想这是一个奇怪的部分,因为这些行是我的grabSheetData函数所在的位置…..但该函数工作正常,我知道每个函数都需要调用authorize()来使用我的令牌获得授权,但authorize函数从第34-46行开始,看起来错误发生在这一行const{client\u secret,client\u id,redirect\u uris}=credentials.installed因为凭证
是未定义的
<代码>凭据
作为参数传递给授权(凭据)
。因此,在您可以调用authorize的地方,您没有传递正确的凭据。在调用authorize(credentials)
的地方没有显示代码,因此在这方面我们没有什么可以帮助的。这并不奇怪。请记住查找错误的含义:未处理PromisejectionWarning
表示您在某处有承诺,但您忘记了.catch(…)
:在100%的情况下,这保证会导致错误,因此浏览器(和节点)会在您有时向您发出警告。然后(…)
没有.catch(…)
。在这种情况下,等待“事情”,但不要访问。数据
,直到等待解决并绑定到响应
。执行此操作时,未经处理的拒绝可能发生在grabSheetData()
中const authClient=wait authorize(creds)代码>authorize()
失败,并且您没有一个try/catch
来处理这个wait
,因此您会收到一个未处理的拒绝错误。但是,根本的问题是,creds
不是它应该是什么。您还应该在await
周围设置一个try/catch
来捕获错误,但这不是这里的根本问题。而且,您仍然没有从一些await
语句中捕获错误。每次等待都会抛出一个被拒绝的承诺。您需要在调用链中的某个位置捕获它们,如果不是本地的,则在调用方中捕获。
app.post('/api/updateSheet', (req,res)=>{
updateSheet()
});
async function updateSheet () {
const authClient = await authorize(creds); //PASS CREDS HERE
const request = {
spreadsheetId: '1VTiTgPvMvLlOVBhBfoMGrshfMMJJMMzoA6GesheWMPg',
range: 'A7:C7',
valueInputOption: 'RAW',
insertDataOption: 'INSERT_ROWS',
resource: {
item: 'dummyname',
dateLogged:'dummydate',
expires:'dummy expire'
},
auth: authClient,
};
try {
const response = (await sheets.spreadsheets.values.append(request)).data;
// TODO: Change code below to process the `response` object:
console.log(JSON.stringify(response, null, 2));
} catch (err) {
console.error(err);
}
}