在函数内部生成的对象不起作用,但是传入的对象起作用

Object made inside function won't work, but object being passed in does

本文关键字:对象 起作用 不起作用 函数 内部      更新时间:2023-09-26

使用Google's Gmail API, A模块返回我在listLabels函数内的Gmail object中使用的auth对象。当它传入它的工作只是很好,但如果我尝试创建完全相同的对象内部的函数和使用Gmail object它返回这个(单独的googleapi模块)

error:     req = authClient.request(options, callback);
                     ^
TypeError: Object #<Object> has no method 'request'

我的函数现在是这样的:

function listLabels(auth) {
    var auth1 = {
        "transporter": {},
        "clientId_": "75i4354355NOTID.apps.googleusercontent.com",
        "clientSecret_": "NOTSECRET",
        "redirectUri_": "http://notawebsite",
        "opts": {},
        "credentials": {
            "access_token": "not.not_access_token",
            "token_type": "Bearer",
            "expiry_data":1441095644613
        }
    }
        console.log("Original Auth: " + JSON.stringify(auth, null, 4));
        console.log("New Auth: " + JSON.stringify(auth1, null, 4));
        var gmail = google.gmail('v1');
        gmail.users.labels.list({
                auth: auth,
                userId: 'email@email.com',
        }, function(err, response) {
                if (err) {
                        console.log('The API returned an error: ' + err);
                        return;
                }
                var labels = response.labels;
                if (labels.length == 0) {
                        console.log('No labels found.');
                } else {
                        console.log('Labels:');
                        for (var i = 0; i < labels.length; i++) {
                                var label = labels[i];
                                console.log('- %s', label.name);
                        }
                }
        });
}

如果我使用传入的auth对象,它工作得很好,如果我使用auth1,它不工作,给我上面的错误。

如您所见,我也尝试打印出下面的两个对象:

Original Auth: {
    "transporter": {},
    "clientId_": "...",
    "clientSecret_": "...",
    "redirectUri_": "...",
    "opts": {},
    "credentials": {
        "access_token": "...",
        "token_type": "Bearer",
        "expiry_date": 1441098460931
    }
}
New Auth: {
    "transporter": {},
    "clientId_": "...",
    "clientSecret_": "...",
    "redirectUri_": "...",
    "opts": {},
    "credentials": {
        "access_token": "...",
        "token_type": "Bearer",
        "expiry_data": 1441095644613
    }
}

(两个令牌都已过期)

记录Auth时:

{ array: 
   [ { [Function: OAuth2Client]
       super_: [Function: AuthClient],
       GOOGLE_OAUTH2_AUTH_BASE_URL_: 'https://accounts.google.com/o/oauth2/auth',
       GOOGLE_OAUTH2_TOKEN_URL_: 'https://accounts.google.com/o/oauth2/token',
       GOOGLE_OAUTH2_REVOKE_URL_: 'https://accounts.google.com/o/oauth2/revoke',
       GOOGLE_OAUTH2_FEDERATED_SIGNON_CERTS_URL_: 'https://www.googleapis.com/oauth2/v1/certs',
       CLOCK_SKEW_SECS_: 300,
       MAX_TOKEN_LIFETIME_SECS_: 86400,
       ISSUER_: 'accounts.google.com' },
     [Function: AuthClient],
     [Function: Object] ],
  string: 'OAuth2Client :: AuthClient :: Object' }

您可能没有打印整个原型链。据我所知,console.log默认情况下不会这样做。所以最初的auth有一些带有request方法的原型,而你的"克隆"没有。因此出现了错误。

或者您只是简单地打印它而不带方法,而auth直接具有request方法。但我认为不打印原型更有可能。