簡體   English   中英

錯誤:文件 index.js 定義的 Node.js 模塊應該導出名為 xxxx 的函數

[英]Error: Node.js module defined by file index.js is expected to export function named xxxx

你好,開發社區。 我正在嘗試調試 firebase 函數並嘗試使用幾個教程,但沒有成功......

我試過( https://medium.com/@mwebler/debugging-firebase-functions-with-vs-code-3afab528bb36 )( https://medium.com/@david_mccoy/build-and-debug-firebase-函數在 vscode-73efb76166cf

我的目的是獲取谷歌聯系人。

函數/index.js

const { google } = require('googleapis');
const oauthUserCredential = require('./oauthUserCredential.json')
const OAuth2 = google.auth.OAuth2

const key = require('./serviceAccountKey.json')
const jwt = new google.auth.JWT(key.client_email, null, key.private_key, 'https://www.googleapis.com/auth/contacts')

exports.getGoogleContacts = functions.https.onCall(async (data, context) => {

  const requestingUser = data.requestingUser
  console.log('getGoogleContacts-requestingUser', requestingUser)


  const oauth2Client = new google.auth.OAuth2(
    'client_id',
    'client_secret',
    'http://localhost:5000/xxx-xxx/us-central1/OAuthCallbackUrl'
  );


  const contacts = google.people({
    version: 'v1',
    auth: oauth2Client,
  });

  console.log('contacts ?', contacts)

    (async () => {
      const { data: groups } = await contacts.people.get({
        resourceName: 'contactGroups',
      });
      console.log('Contact Groups:\n', groups);

    })()


  jwt.authorize((err, response) => {
    console.log('inside authorize')
    if (err) {
      console.error(err);
      response.end();
      return;
    }

    // Make an authorized request to list contacts.
    contacts.people.connections.list({
      auth: authClient,
      resourceName: 'people/me'
    }, function (err, resp) {
      if (err) {
        console.error(err);
        response.end();
        return;
      }

      console.log("Success");
      console.log(resp);
      response.send(resp);
    });

  });

  // this is another approach I´ve tried, but it´s also not working

  const oAuth2Client = new OAuth2(
    oauthUserCredential.web.client_id,
    oauthUserCredential.web.client_secret,
    oauthUserCredential.web.redirect_uris,
)

oAuth2Client.setCredentials({
    refresh_token: oauthUserCredential.refresh_token
})
return new Promise((resolve, reject) => {
    console.log('[INSIDE PEOPLE CONNECTIONS]')
    contacts.people.connections.list({
        auth: oauth2Client //authetication object generated in step-3
    }, function (err, response) {
        if (err) {
            console.log('contacts.people.connections error')
            console.log(err)
            reject(new Error(err))
        } else if (response) {
            console.log('contacts.people.connections response')
            console.log(response)
            resolve(response)
        }
    });
})
    .then(result => { return { found: result } })
    .catch(err => { return { error: err } })


})

我嘗試了幾種不同的方法並遵循了不同的教程( 使用 Google People API 和 Cloud Functions for Firebase )( https://flaviocopes.com/google-api-authentication/ )( https://medium.com/@smccartney09/ integration-firebase-cloud-functions-with-google-calendar-api-9a5ac042e869 ) ( https://cloud.google.com/community/tutorials/cloud-functions-oauth-gmail )

但他們都沒有清楚地顯示我如何獲得我的聯系人列表。 我可以按照本教程( https://labs.magnet.me/nerds/2015/05/11/importing-google-contacts-with-javascript.html )使用客戶端代碼,但我認為生活在客戶端暴露的 client_id、client_secret 和 apiKey 將是一個安全問題......

在此處輸入圖片說明

我還提交了一個教程請求,以明確如何使用 firebase 函數從谷歌帳戶獲取聯系人列表。

您收到的錯誤是因為雲函數找不到名為 xxxx 的函數來執行,因為您尚未在 index.js 文件中定義任何名為 xxxx 的函數。

您的 Cloud Function 執行名稱,根據錯誤消息是 xxxx 但您在 index.js 中調用的函數是 getGoogleContacts。 請確保這些名稱相同,例如將 getGoogleContacts 更改為 xxxx 或將執行的函數更改為 getGoogleContacts

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM