如何使用 Cloud Function 获取 Firestore 集合?

14079_Z

我是后端开发的新手,正在尝试编写一个可以在 FireStore 中读取、写入和编辑数据的云函数。我做了用于写入的云功能(添加新数据)。但是,我无法读取工作。如果您能帮助我阅读和编辑,我将不胜感激。谢谢!

Firestore structure:

UserProfile
     |
     +--wWXaLpiOKegAMw6Jhsdf -- {"name": "Danny", "dob": "01/01/2000"}
     |
     |
     +--Ksfe7segse8tPs2pe1Qu -- {"name": "David", "dob": "11/29/1994"}
     |
     |
     +--Udfuwoi8se9g7rdw65dh -- {"name": "Sherry", "dob": "07/21/1997"}
     .
     .
     .


//index.js
const functions = require("firebase-functions");
const admin = require('firebase-admin');
admin.initializeApp();

const edit = require('./edit.js');
const read = require('./read.js');

exports.edit = functions.https.onRequest((request, response) => {
    return edit.edit(request, response);
});

exports.read = functions.https.onRequest((response) => {
     return read.read(response);
});


//read.js
const functions = require('firebase-functions');
const admin = require('firebase-admin');

exports.read = functions.https.onRequest(async (response) => {
    await admin.firestore().collection('UserProfile').get().then(snapshot => {

        snapshot.forEach(doc => {
            let arr = {
                name: doc.data().name,
                dob: doc.data().dob
            }
            response.json({result: arr });
        })
    })
})

错误:“TypeError:response.json 不是函数”

//edit.js
const functions = require('firebase-functions');
const admin = require('firebase-admin');
    
exports.edit = functions.https.onRequest(async (request, response) => {
await admin.firestore().collection('UserProfile').get().then(snapshot => {
    
    snapshot.forEach(doc => {
       if (request.body.id == doc.id) {
         doc.data().name = request.body.name,
         doc.data().dob = request.body.dob
       } 
            
       const result = {
         name = request.body.name,
         dob = request.body.dob
       }                 
       response.json({result: result});
       })
    })
 })

我还没有尝试编辑,所以可能存在像 read 这样的语法问题。我想比较文档 ID 以进行编辑。

道格史蒂文森

您需要传递和使用这两个参数,而不仅仅是response. JavaScript 有位置参数,而不是命名参数。现在,您的代码正在尝试使用 Request 对象作为 Response 对象,因为您将第一个 Request 对象命名为response

此外,您不需要使用函数 SDK 来声明您的读取导出。只需使用正常功能即可。只有顶级索引导出需要使用功能SDK builder。

索引.js

exports.read = functions.https.onRequest((request, response) => {
     return read.read(request, response);
});

读取.js

exports.read = async (request, response) => { ... }

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何使用Cloud Firestore获取集合中文档的数量

SwiftUI:如何迭代 Cloud Firestore 集合中的文档并获取数据?

如何获取集合Cloud Firestore中的文档ID列表?

如何从Android中的Cloud Firestore获取集合内的对象列表?

如何根据索引从 Cloud Firestore 集合中获取文档?

获取Cloud Firestore数据库集合

如何在Cloud Firestore中高效地获取集合及其子集合的所有文档

如何使用 Swift 从 Firebase Cloud Firestore 获取集合中的最新文档

Firestore Cloud Function空集合

如何使用模块化 Web SDK 的版本 9 在 Cloud Firestore 集合中的文档中获取文档和嵌套集合?

如何通过Android Cloud Firestore中的集合过滤器获取文档数

如何在Firebase / Google Cloud Firestore的集合中获取最新添加的文档?

Firebase Cloud Firestore的成本优化模式:通过集合获取文档

获取文档Cloud Firestore中集合的所有文档

Firestore-如何从DocumentSnapshot获取集合?

如何获取firestore集合下的文档数量?

如何获取firestore集合中的文档数

如何从then()函数中获取Firestore集合引用?

如何使用Flutter从Firestore中的集合中获取特定文档

如何使用Firebase Firestore获取子集合?

如何在Cloud Firestore中有效地获取不同集合中具有特定ID的文档?

Cloud Firestore 通过引用从另一个集合中获取集合

如何从Cloud Function访问Cloud Firestore?

我可以使用firebase_functions_interop从Cloud Function向Firestore进行“集合组查询”吗?

如何使用Flutter获取Firestore中每个集合的子集合

如何在Cloud Flutter Firestore中处理嵌套集合查询?

如何在Cloud Firestore中创建子集合索引?

如何从 Cloud Firestore 数据库中检索数据集合

如何使用graphql从Firebase使用flutter从Cloud Firestore获取数据?