当试图将数据发送到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)
我的索引.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}
我在消防商店的收藏:在此处输入图像描述
您需要进行
const db = admin.firestore();
而不是
const db = admin.firestore;
请注意,由于Firebase SDK for Cloud Functions的1.0.0版本,firebase-admin
应在没有任何参数的情况下进行初始化,请参阅https://firebase.google.com/docs/functions/beta-v1-diff#new_initialization_syntax_for_firebase-管理员。