Pancho
Pancho

Reputation: 110

Write data Firestore using callable function

How to write data to Firestore with callable function

I'm trying to make a function that receives data within the same http call to pass it to Firestore. Basing myself off of the Firebase quickstart for Add Message So far so good with this 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".
That triggers the function and sends data to 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.

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

This is the result of running that function:
example_of_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 -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)"

If not something like above possible, do you all know how can this be accomplished?
Help is greatly appreciated!

Upvotes: 0

Views: 77

Answers (1)

Pancho
Pancho

Reputation: 110

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.
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.

Upvotes: 0

Related Questions