Koa Typescript应用程序会挂在任何Firebase数据库调用上 [英] Koa Typescript app hangs on any Firebase db call
问题描述
我有一个简单的 koa
REST 服务,该服务以 typescript 编写,具有 GET
请求,只需返回所有db ref /user
的 firebase数据库中的用户.
I have a simple koa
REST service wrote in typescript that has a GET
request the simply returns all users from a firebase database at db ref /user
.
应用程序接收到该请求并获得了数据库引用,但是在应获取该值时它会永久挂起.
The request gets received by the app and it gets the database reference, but it hangs forever when it should get the value.
这是我的 index.ts
文件:
import * as Koa from "koa";
import * as Router from "koa-router";
import * as json from "koa-json";
import * as bodyParser from "koa-bodyparser";
import firebase from "firebase";
import { config } from "./config";
const app = new Koa();
const router = new Router();
router.get("/users", async (ctx, next) => {
firebase.initializeApp(config.firebaseConfig);
const db = firebase.database();
const ref = db.ref("user");
console.log("got ref");
const snapshot = await ref.once("value");
console.log("got snapshot");
ctx.body = { users: snapshot.val() };
await next();
});
app.use(json());
app.use(bodyParser());
app.use(router.routes()).use(router.allowedMethods());
app.listen(3000, () => {
console.log("Koa started on port 3000");
});
这是输出:
Koa started on port 3000
<-- GET /users
got ref
[2021-02-21T10:09:03.818Z] @firebase/database: FIREBASE WARNING: Namespace [masked-my-project]-dev-default-rtdb lives in a different region. Please change your database URL to https://[masked-my-project]-dev-default-rtdb.europe-west1.firebasedatabase.app (https://[masked-my-project]-dev-default-rtdb.firebaseio.com/)
这是我的 tsconfig.json
:
{
"version": "2.0.2",
"compilerOptions": {
"outDir": "./dist",
"lib": ["ES5", "ES6"],
"noImplicitReturns": true,
"sourceMap": true,
"target": "ES6",
"module": "commonjs",
"moduleResolution": "node",
"emitDecoratorMetadata": true,
"experimentalDecorators": true
},
"include": ["./src/**/*"],
"files": ["node_modules/typescript/lib/lib.es6.d.ts"],
"exclude": ["node_modules"]
}
package.json
:
{
"name": "koa-new",
"version": "1.0.0",
"description": "",
"main": "dist/index.js",
"scripts": {
"dev": "./node_modules/.bin/tsc-watch --onSuccess \"node .\"",
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"dotenv": "^8.2.0",
"firebase": "^8.2.9",
"koa": "^2.13.1",
"koa-bodyparser": "^4.3.0",
"koa-json": "^2.0.2",
"koa-logger": "^3.2.1",
"koa-route": "^3.2.0",
"koa-router": "^10.0.0",
"ts-node": "^9.1.1",
"typescript": "^4.1.5"
},
"devDependencies": {
"@types/dotenv": "^8.2.0",
"@types/firebase": "^3.2.1",
"@types/koa": "^2.13.0",
"@types/koa-bodyparser": "^4.3.0",
"@types/koa-json": "^2.0.18",
"@types/koa-logger": "^3.1.1",
"@types/koa-router": "^7.4.1",
"@types/node": "^14.14.31",
"tsc-watch": "^4.2.9"
}
}
.env
:
FIREBASE_API_KEY=[mask]
FIREBASE_AUTH_DOMAIN=[mask].firebaseapp.com
FIREBASE_DATABASE_URL=https://[mask]-default-rtdb.europe-west1.firebasedatabase.app
FIREBASE_PROJECT_ID=[mask]
FIREBASE_STORAGE_BUCKET=[mask].appspot.com
FIREBASE_MESSAGING_SENDER_ID=[mask]
FIREBASE_APP_ID=1:[mask]:web:[mask]
FIREBASE_MEASUREMENT_ID=[mask]
推荐答案
问题是 firebaseConfig
在名为 databaseUrl
而不是的属性中具有数据库url.databaseURL
并且由于"Url"不是大写的firebase,以为我忘了添加它,并且默认为 https://[masked-my-project] -dev-default-rtdb.firebaseio.com/
,而它需要为https://[masked-my-project] -dev-default-rtdb.europe-west1.firebasedatabase.app
,因此出现错误消息.
The problem was that firebaseConfig
had the db url in a property named databaseUrl
instead of databaseURL
and because of "Url" not being uppercased firebase thought I forgot to add it and defaulted to https://[masked-my-project]-dev-default-rtdb.firebaseio.com/
whereas it needed to be https://[masked-my-project]-dev-default-rtdb.europe-west1.firebasedatabase.app
therefore the error message.
这篇关于Koa Typescript应用程序会挂在任何Firebase数据库调用上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!