我是后端开发的新手,正试图编写一个可以在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 });
})
})
})
error: "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有位置参数,而不是命名参数。现在,你的代码正试图使用一个请求对象作为一个响应对象,因为你将第一个请求对象命名为response
。
同样,您不需要使用SDK函数来声明您的read导出。用一个普通的函数。只有顶层索引导出需要使用函数SDK构建器。
index.js
exports.read = functions.https.onRequest((request, response) => {
return read.read(request, response);
});
read.js
exports.read = async (request, response) => { ... }