Help us understand the problem. What is going on with this article?

Firebase FunctionsでRealtime databaseにdataを送信する

恒例

https://firebase.google.com/docs/functions ドキュメントを読んでください

Firebase CLI をインストールする

npm install -g firebase-tools

Coding

mkdir test
cd test
mkdir functions
cd functions
vi package.json
{
  "name": "mytest",
  "description": "mytest",
  "dependencies": {
    "firebase-admin": "~7.1.1",
    "firebase-functions": "^2.2.1"
  },
  "devDependencies": {
    "chai": "^3.5.0",
    "chai-as-promised": "^6.0.0",
    "firebase-functions-test": "0.1.6",
    "mocha": "^5.0.5",
    "sinon": "^4.1.3"
  },
  "scripts": {
    "ci-test": "npm install && npm run test",
    "serve": "firebase serve --only functions",
    "shell": "firebase experimental:functions:shell",
    "start": "npm run shell",
    "deploy": "firebase deploy --only functions",
    "logs": "firebase functions:log"
  },
  "engines": {
    "node": "8"
  },
  "private": true
}
vi index.js
'use strict';
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();

exports.send_data = functions.https.onRequest(async (req, res) => {

  const time = req.query.time;
  const text = req.query.temp;

  const snapshot = await admin.database().ref('/test').push({time:time,text:text});

  res.redirect(303, snapshot.ref.toString());
});

exports.read_data = functions.https.onRequest((req, res) => {

    var ref = admin.database().ref("test");
    return ref.orderByChild("time").limitToLast(10).once('value').then(function(snapshot) {
      res.send(snapshot.val());
    });

});

Deploy

cd functions && npm install && cd ..
firebase deploy

送信方法

https://us-central1-[MY_PROJECT].cloudfunctions.net/send_data?time=20200218&text=uppercaseme をアクセスする

DATAを読み取る方法

https://us-central1-[MY_PROJECT].cloudfunctions.net/read_data をアクセスする

yon_engineer
クローリング仕事をやっている
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした