【发布时间】:2020-08-27 13:42:36
【问题描述】:
当尝试将数据发送到 Firestore 时,您将返回此错误:
TypeError: db.collection is not a function
at FirestoreHelper.updateDocument (/workspace/node_modules/firebase-functions-helper/dist/firestore.js:7:56)
at app.patch (/workspace/lib/index.js:20:40)
at Layer.handle [as handle_request] (/workspace/node_modules/express/lib/router/layer.js:95:5)
at next (/workspace/node_modules/express/lib/router/route.js:137:13)
at Route.dispatch (/workspace/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/workspace/node_modules/express/lib/router/layer.js:95:5)
我的 index.ts
import * as functions from 'firebase-functions';
import * as admin from 'firebase-admin';
import * as firebaseHelper from 'firebase-functions-helper/dist';
import * as express from 'express';
import * as bodyParser from 'body-parser';
admin.initializeApp(functions.config().firebase);
const db = admin.firestore;
const app = express();
const main = express();
main.use(bodyParser.json());
main.use(bodyParser.urlencoded({extended: false}));
main.use('/api/v1', app);
const trackerCollection = 'tracker';
export const webApi = functions.https.onRequest(main);
app.patch('/trackers/:trackerId', async(req, res) => {
try {
await firebaseHelper.firestore.updateDocument(db, trackerCollection, req.params.trackerId, req.body);
res.status(200).send('Update Success');
} catch (error) {
console.log(error);
res.status(204).send('Patch Error');
}
})
我正在为邮递员试镜。 api url 为:https://myapp.../api/v1/trackers/12345678
状态邮递员 = 204
- 身体 - 原始: { “经度”:47 }
我在 Firestore 中的收藏: enter image description here
【问题讨论】:
标签: javascript node.js firebase google-cloud-firestore google-cloud-functions