typescript 如何通过firebase函数将数据写入firestore?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/47726099/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to write data to firestore through firebase functions?
提问by tweetypi
I can quite easily find how to add triggers for firestore events but I can't find any documentation on how to use firebase functions to write to firestore.
我可以很容易地找到如何为 firestore 事件添加触发器,但我找不到任何关于如何使用 firebase 函数写入 firestore 的文档。
For example I have the following:
例如,我有以下内容:
export const listener = functions.auth.user().onCreate((x)=>{
//firestore function to add an entry for user with fields...
})
There doesn't seem to be a way to do this? Is it that the logic for adding data to firestore should be written client side? If so wouldn't that mean I would need to re-write that functionality for every platform?
似乎没有办法做到这一点?向firestore添加数据的逻辑应该写在客户端吗?如果是这样,那是不是意味着我需要为每个平台重新编写该功能?
回答by Doug Stevenson
You can use the Firebase Admin SDKto read and write Firestore in the same way you'd use it to read and write Realtime Database. There are plenty of official examples, especially this quickstart.
您可以使用Firebase Admin SDK来读取和写入 Firestore,就像使用它来读取和写入实时数据库一样。有很多官方示例,尤其是这个快速入门。
const admin = require('firebase-admin');
admin.initializeApp();
admin.firestore().collection('messages').add({original: original}).then(writeResult => {
// write is complete here
});