简体   繁体   English

使用可调用 function 写入数据 Firestore

[英]Write data Firestore using callable function

How to write data to Firestore with callable function如何使用 callable function 将数据写入 Firestore

I'm trying to make a function that receives data within the same http call to pass it to Firestore.我正在尝试制作一个 function,它在同一个 http 调用中接收数据以将其传递给 Firestore。 Basing myself off of the Firebase quickstart for Add Message So far so good with this function:Firebase 快速入门为基础添加消息到目前为止,这个 function 一直很好:

exports.sendingMessage = functions.https.onRequest(async (req, res) => {
  var wtf = new Date();
  var month = wtf.getMonth();
  var date = wtf.getDate();
  var year = wtf.getFullYear();

  var DATE = (month + "-" + date + "-" + year);
  var type = "announcement"
  const original = req.query.text;

  var dataref = {
        type: type,
        date:DATE,
        update:original   
                }

  const myDB = await admin.firestore().collection('unfiltered').add({dataref:dataref});

  console.log('Added document with ID: ', myDB.id);


  res.end()
});

The way the function works is by calling it (curling it) and putting a text where it says "TEXT_HERE". function 的工作方式是调用它(卷曲它)并将文本放在它显示“TEXT_HERE”的位置。
That triggers the function and sends data to FireStore:这会触发 function 并将数据发送到 FireStore:
Below is an example of how the function looks, I changed the project name for privacy purposes, this one works fine, but passes only one word to the database.下面是 function 的示例,出于隐私目的我更改了项目名称,这个工作正常,但只向数据库传递了一个词。

curl https://us-central1-MY-PROJECT-ID.cloudfunctions.net/sendingMessage?text=TEXT_HERE -H "Authorization: bearer $(gcloud auth print-identity-token)" curl https://us-central1-MY-PROJECT-ID.cloudfunctions.net/sendingMessage?text=TEXT_HERE -H “授权:bearer $(gcloud auth print-identity-token)”

This is the result of running that function:这是运行 function 的结果:
函数示例

I'd like to pass on more than a single word, this is just speculation I tried the curl below expecting it'd let me send a whole sentence to Firestore like this, but not much luck:我想传递不止一个词,这只是猜测我尝试了下面的 curl 希望它能让我像这样向 Firestore 发送一个完整的句子,但运气不佳:

curl -X POST --header "Content-Length: 1000" https://us-central1-MY-PROJECT-ID.cloudfunctions.net/sendingMessage?=-d '{"text":"Lorem ipsum dolor sit amet"}' -H "Authorization: bearer $(gcloud auth print-identity-token)" curl -X POST --header "Content-Length: 1000" https://us-central1-MY-PROJECT-ID.cloudfunctions.net/sendingMessage?=-d '{"text":"Lorem ipsum dolor sit amet" }'-H“授权:持有者$(gcloud auth print-identity-token)”

If not something like above possible, do you all know how can this be accomplished?如果不可能像上面那样,你们都知道如何实现吗?
Help is greatly appreciated!非常感谢您的帮助!

This was due to lack of understanding, sorry about my lack of knowledge!这是由于缺乏了解,抱歉我缺乏知识!
The thing here is that node.js takes this variable as an html encoded query.这里的事情是 node.js 将此变量作为 html 编码查询。
So if you're querying for the keyword "text" it's actually a variable, and should be treated as an html encoded variable, so it doesn't take spaces, it works with the symbol %20 as space.因此,如果您要查询关键字“text”,它实际上是一个变量,应该被视为 html 编码变量,因此它不占用空格,它使用符号 %20 作为空格。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM