【问题标题】:Error: Node.js module defined by file index.js is expected to export function named xxxx错误:文件 index.js 定义的 Node.js 模块应导出名为 xxxx 的函数
【发布时间】:2019-08-04 08:50:28
【问题描述】:

您好,开发者社区。我正在尝试调试 firebase 函数并尝试使用多个教程,但没有成功...

我试过了 (https://medium.com/@mwebler/debugging-firebase-functions-with-vs-code-3afab528bb36) (https://medium.com/@david_mccoy/build-and-debug-firebase-functions-in-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 } })


})

我尝试了几种不同的方法并遵循不同的教程 (Using Google People API with Cloud Functions for Firebase) (https://flaviocopes.com/google-api-authentication/) (https://medium.com/@smccartney09/integrating-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 功能从 google 帐户获取联系人列表。

【问题讨论】:

    标签: javascript firebase google-contacts-api


    【解决方案1】:

    您收到的错误是因为云函数找不到要执行的名为 xxxx 的函数,因为您没有在 index.js 文件中定义任何名为 xxxx 的函数。

    您要执行的云函数名称,根据错误消息是 xxxx,但您在 index.js 中调用的函数是 getGoogleContacts。请确保这些名称相同,例如将 getGoogleContacts 更改为 xxxx 或将要执行的函数更改为 getGoogleContacts

    【讨论】:

      猜你喜欢
      • 2015-09-25
      • 2017-02-20
      • 1970-01-01
      • 2021-11-10
      • 2020-01-26
      • 1970-01-01
      • 2017-06-13
      • 1970-01-01
      • 2019-10-28
      相关资源
      最近更新 更多