簡體   English   中英

firebase 函數模擬器:context.auth 未定義

[英]firebase functions emulator: context.auth is undefined

我們正在使用模擬器來創建一些本地測試環境。 我們遇到了 context.auth 總是未定義的問題。

謝謝!

const functions = require('firebase-functions');
const util = require('util');

exports.helloWorld = functions.https.onCall(async (data, context) => {
  console.log("helloWorld", util.inspect(context.auth));
  return {
    auth: context.auth
  };
});

這是腳本代碼

 <script type="module">
    import { initializeApp } from 'https://www.gstatic.com/firebasejs/9.8.4/firebase-app.js';
    import {getAuth,connectAuthEmulator, signInAnonymously } from 'https://www.gstatic.com/firebasejs/9.8.4/firebase-auth.js';
    import { getFunctions, connectFunctionsEmulator, httpsCallable } from 'https://www.gstatic.com/firebasejs/9.8.4/firebase-functions.js';
    initializeApp({
      projectId: 'demo-project1',
      apiKey: 'dummy',
    });
    document.addEventListener('DOMContentLoaded', async function () {
      const auth = getAuth();
      connectAuthEmulator(auth, "http://localhost:9099");
      const functions = getFunctions();
      connectFunctionsEmulator(functions, "localhost", 6001);
      auth.onAuthStateChanged(user => {
        console.log("USER CHANGED TO ", user);
        if (user) {
          callFunction();
        }
      });
      await signInAnonymously(auth);
    });

    async function callFunction() {
      const functions = getFunctions();
      const helloWorld = httpsCallable(functions, 'helloWorld');
      let result = await helloWorld({ text: "hello" });
      console.log(result.data);
    };
  </script>

完整示例也在 GH https://github.com/eran-swimm/test-emulators相關票證https://github.com/firebase/firebase-tools/issues/4690

問題是缺少安裝 firebase-admin。 運行時,模擬器給了我這些警告。

The Cloud Functions emulator requires the module "firebase-admin" to be installed as a dependency. To fix this, run "npm install --save firebase-admin" in your functions directory.

i  functions: Your functions could not be parsed due to an issue with your node_modules (see above)

暫無
暫無

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

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