0

运行时出现此错误npm run dev

registerFunctions(firebase$1, fetch.bind(self));
                              ^
ReferenceError: fetch is not defined

我认为rxfire不会导入 fetch,所以我将此行添加到src/server.ts

global['fetch'] = require('node-fetch');

错误仍然存​​在,有什么建议吗?我想我根本不需要添加这个。

我只是在src/ firebase.ts 文件中使用rxfire ,如下所示:

import firebase from "firebase/app";
import "firebase/auth";
import "firebase/firestore";
import "firebase/functions"; 
import * as config from "./config.json";

firebase.initializeApp(config);

export const auth = firebase.auth();
export const googleProvider = new firebase.auth.GoogleAuthProvider();

export const db = firebase.firestore();
export const functions = firebase.functions();

谢谢,
J

4

3 回答 3

0

这是尝试import "firebase/functions"在我的firebase.ts文件中的服务器端时导致的特定错误。

我能够通过添加import 'isomorphic-unfetch'server.ts来解决它。但是,然后我收到了这个错误:

registerFunctions(firebase$1, fetch.bind(self));
                                         ^

ReferenceError: self is not defined

我意识到后端显然没有要绑定的对象,所以我找到了解决方法。我不需要在后端导入它,这是一个更大的 Sapper Firebase 问题。

在这里查看我的帖子以获得完整的修复。

于 2021-02-15T04:56:33.740 回答
0

我对这个错误堆栈跟踪有类似的问题:

ReferenceError: fetch is not defined
    at Module.<anonymous> (/var/task/webpack:/Users/dev/projects/node_modules/@firebase/functions/dist/index.esm.js:702:27)
at __webpack_require__ (/var/task/webpack:/webpack/bootstrap:19:1)
at /var/task/webpack:/webpack/bootstrap:83:1
at Object.<anonymous> (/var/task/families.js:87:10)
at Module._compile (internal/modules/cjs/loader.js:1072:14)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1101:10)
at Module.load (internal/modules/cjs/loader.js:937:32)
at Function.Module._load (internal/modules/cjs/loader.js:778:12)
at Module.require (internal/modules/cjs/loader.js:961:19)
at Module._require.i.require (/var/task/serverless_sdk/index.js:9:73397)

我通过将 firebase 导入更新为

import * as admin from 'firebase-admin';
import firebase from 'firebase/app';
于 2021-08-27T11:17:48.753 回答
0

first install whatwg-fetch.

npm i --save whatwg-fetch yarn add --save whatwg-fetch pnpm i --save whatwg-fetch

create a file ./define-self.js

with this content

var global =
  (typeof globalThis !== "undefined" && globalThis) ||
  (typeof self !== "undefined" && self) ||
  (typeof global !== "undefined" && global);

if (!global?.self) {
  global.self = global;
}

on your index.svelte / _layout.svelte

<script context="module">
  import "./define-self";
  import "whatwg-fetch";
</script>
于 2022-01-20T20:58:55.893 回答