【问题标题】:Cloud functions error : TypeError: db.collection is not a function云函数错误:TypeError:db.collection 不是函数
【发布时间】: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


    【解决方案1】:

    你需要做的

    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-admin。

    【讨论】:

      猜你喜欢
      • 2017-10-02
      • 1970-01-01
      • 1970-01-01
      • 2017-12-23
      • 1970-01-01
      • 1970-01-01
      • 2023-04-01
      • 2020-11-05
      相关资源
      最近更新 更多