如何从Firebase Cloud Function写入Firestore文档



我正在尝试制作一个云函数。每当我试图达到终点时,我都会得到500 Internal Server Error邮差回应图片在这里

我已经检查了firebase函数的日志,也没有看到任何信息。它只是说";函数崩溃";而没有任何进一步的信息。

我还检查了Firestore数据库结构中的任何拼写错误和不匹配,但对我来说一切都很好

这是我在firebase项目上传的firebase函数的代码。


const functions = require('firebase-functions');
const admin = require('firebase-admin');
const { error } = require('firebase-functions/lib/logger');
admin.initializeApp(functions.config().firebase);

exports.addEvent = functions.region('asia-east2').https.onRequest(async (req, res) => {
if (req.method === 'POST') {
var db = admin.firestore();
var write = db.collection("Colleges")
.doc(req.body.college)
.collection("events")
.doc(req.body.event.id)
.set({
id: req.body.event.id,
admin: req.body.event.admin,
event_state: req.body.event.event_state,
name: req.body.event.name,
poster_url: req.body.event.poster_url,
start_date_time: req.body.event.start,
end_date_time: req.body.event.end,
location: req.body.event.location,
short_desc: req.body.event.shortDesc,
long_desc: req.body.event.longDesc,
contacts: req.body.event.contacts,
links: req.body.event.links,
});
return res.send(write);
}
else
return res.sendStatus(403);
});

这是我从邮差发送的POST请求的正文

{
"college": "college_name",
"event": {
"id": 1234,
"admin": "admin",
"event_state": 2,
"name": "Event Name",
"poster_url": "test",
"start": "Date Time",
"end": "Date Time",
"location": "auditorium",
"shortDesc": "lorem ipsum short",
"longDesc": "lorem ipsum long",
"contatcs": [
{
"tag": "Name Tag",
"contact": 12345678
}
],
"links": [
{
"tag": "Link Tag",
"link": 123456784
}
]
}
}

Firestore结构有点像

-Colleges (Collection)
|
|
-Document 
|
-events(Collection)
|
-Event Documents (Document which i want to write to ,from the firebase function)

问题是负载中的事件id是number,Firestore文档id必须是字符串。因此,您要么使用.doc(req.body.event.id.toString()),要么将事件id作为字符串发送到负载id: "1234"中。

此外,请考虑按照Firebase准则重构代码以处理POST方法。

相关内容

  • 没有找到相关文章

最新更新