gpt4 book ai didi

node.js - 无法解构属性错误 - googleapi

转载 作者:太空宇宙 更新时间:2023-11-04 01:48:28 34 4
gpt4 key购买 nike

使用googleapi时出错

我的错误:

(node:7776) UnhandledPromiseRejectionWarning: TypeError: Cannot destructure property data of 'undefined' or 'null'. at gmail.users.labels.list

令我困惑的是,一切正常,它按应有的方式打印出来,但如果它显然不是,为什么它会给我一个关于为 null 的错误?怎么解决这个问题?

这是我的代码:

var async = require('async-kit');
const fs = require('fs');
const readline = require('readline');
const {
google
} = require('googleapis');

const SCOPES = ['https://www.googleapis.com/auth/gmail.readonly'];
const TOKEN_PATH = 'credentials.json';

function test(methodname) {
console.log('called');
fs.readFile('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.
authorize(JSON.parse(content), methodname);
});
}

/**
* Create an OAuth2 client with the given credentials, and then execute the
* given callback function.
* @param {Object} credentials The authorization client credentials.
* @param {function} callback The callback to call with the authorized client.
*/
function authorize(credentials, callback) {
console.log('authorized');
const {
client_secret,
client_id,
redirect_uris
} = credentials.installed;
const oAuth2Client = new google.auth.OAuth2(
client_id, client_secret, redirect_uris[0]);

// Check if we have previously stored a token.
fs.readFile(TOKEN_PATH, (err, token) => {
if (err) return getNewToken(oAuth2Client, callback);
oAuth2Client.setCredentials(JSON.parse(token));
callback(oAuth2Client);
});
}

/**
* Get and store new token after prompting for user authorization, and then
* execute the given callback with the authorized OAuth2 client.
* @param {google.auth.OAuth2} oAuth2Client The OAuth2 client to get token for.
* @param {getEventsCallback} callback The callback for the authorized client.
*/
function getNewToken(oAuth2Client, callback) {
const authUrl = oAuth2Client.generateAuthUrl({
access_type: 'offline',
scope: SCOPES,
});
console.log('Authorize this app by visiting this url:', authUrl);
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
rl.question('Enter the code from that page here: ', (code) => {
rl.close();
oAuth2Client.getToken(code, (err, token) => {
if (err) return callback(err);
oAuth2Client.setCredentials(token);
// Store the token to disk for later program executions
fs.writeFile(TOKEN_PATH, JSON.stringify(token), (err) => {
if (err) return console.error(err);
console.log('Token stored to', TOKEN_PATH);
});
callback(oAuth2Client);
});
});
}

/**
* Lists the labels in the user's account.
*
* @param {google.auth.OAuth2} auth An authorized OAuth2 client.
*/
function listLabels(auth, callback) {
const gmail = google.gmail({
version: 'v1',
auth
});
gmail.users.labels.list({
userId: 'me',
}, (err, {
data
}) => {
if (err) {
console.log('The API returned an error: ' + err);
callback();
}
const labels = data.labels;
console.log('got labels');
if (labels.length) {
interimlabels = labels;
interimlabels.forEach((label) => {
console.log(`- ${label.name}`);
});
callback();
}
else {
console.log('No labels found.');
callback();
}
});
}
var interimlabels = [];
async.series([
function getStuff(callback) {
test(listLabels);
if (interimlabels.length != 0) {
console.log('labels', interimlabels);
callback();
}
}
])
.exec(function(error, results) {
if (error) {
console.log('shit');
}
else {
console.log('done');
}
});

编辑:我只是想补充一点,它完美地工作,按应有的方式注销,但在最后给了我错误之后edit2:好的,如果我删除回调();来自列表标签();然后promiserejection消失了,但是代码显然会卡在那里,那么回调可能有什么问题呢?

最佳答案

//您收到一个错误,并且错误处理中出现了一些错误。你破坏了谷歌 API 的结果。但实际上您没有得到任何结果,因此它显示了给定的错误:Cannot destructory property error - googleapi

//试试这个代码。

/**
* Lists the labels in the user's account.
*
* @param {google.auth.OAuth2} auth An authorized OAuth2 client.
*/
function listLabels(auth, callback) {
const gmail = google.gmail({
version: 'v1',
auth
});
gmail.users.labels.list({
userId: 'me',
}, (err, result) => {
if (err || !result) {
console.log('The API returned an error: ' + err);
callback();
} else {
const labels = result.data.labels;
console.log('got labels');
if (labels.length) {
interimlabels = labels;
interimlabels.forEach((label) => {
console.log(`- ${label.name}`);
});
callback();
}
else {
console.log('No labels found.');
callback();
}
}
});
}

希望这能解决您的疑问。

更新的代码:

function listLabels(auth, callback) {
const gmail = google.gmail({
version: 'v1',
auth
});

var request = gmail.users.labels.list({
'userId': 'me'
});
request.execute(function(resp) {
console.log("resp", resp);
var labels = resp.labels;
callback(labels);
});
}

关于node.js - 无法解构属性错误 - googleapi,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50563860/

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