节点koa REST (typescript)挂起在firebase数据库调用上



我有一个简单的koaRESTtypescript编写的服务它有一个GET请求,简单地从firebase数据库返回所有用户。at db ref/user.

请求被应用程序接收并获得数据库引用,但它永远挂起当它应该得到值

这是我的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]

这看起来是这个问题的重复,所以我将发布与我在那里提供的相同的答案。

我没有在Firebase实时数据库中使用Koa的经验(我曾在Firestore中使用Koa来构建REST api),所以对此持怀疑态度。对我来说,它看起来像你做的一切都是正确的,只要从你的实时数据库中获取相关的数据。但是,我总是将任何想要添加到响应体中的对象进行字符串化,例如:

const responseBody = {
'users' : snapshot.val()
}
ctx.body = JSON.stringify(responseBody);

此外,在您返回的对象中,我认为users应该在引号内(作为对象中的string类型键)。

最后,我还注意到你正在使用koa-bodyparser。Firebase已经解析了主体,koa-bodyparser不能处理已经解析的主体,它将是一个对象,而不是一个未解析的字符串。这可能会导致您的呼叫挂起。而不是使用koa-bodyparser,当你需要访问请求体时,使用ctx.req.body

最新更新