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

Lambdaでfirebase realtime databaseを使う

More than 1 year has passed since last update.

firebase側

firebaseのプロジェクトを作成し、新しい秘密鍵の生成をする。

設定>>プロジェクトの設定>>サービスアカウント>>新しい秘密鍵の生成

izvote_–_設定_–_Firebase_console.png

ダウンロードできたJSONファイルをプロジェクトのどっかに置く。

index.js

初期設定的なやつ。

var admin = require("firebase-admin");

var serviceAccount = require("./serviceAccountKey.json");

// appNameをプロジェクト名にする
admin.initializeApp({
  credential: admin.credential.cert(serviceAccount),
  databaseURL: "https://appName.firebaseio.com"
});
var db = admin.database();
var ref = db.ref("/path");

データの保存

set データの書き込みor置換

/users/以下に保存
子ノード全てを更新してしまうので、既存のデータも上書きしてしまう。更新にはupdateを使う

var usersRef = ref.child("users");
await usersRef.set({
  user01: {
    name: "imajo"
  }
});

update データの更新

var usreRef = usersRef.child("user");
userRef.update({
  "nickname": "imajojo"
});

取得

once

await usersRef.once("value", function(snapshot) {
  console.log(snapshot.val());
}, function (errorObject) {
  console.log("The read failed: " + errorObject.code);
});

パラメータ(クエリ)指定して取得する方法は以下
https://firebase.google.com/docs/database/admin/retrieve-data?authuser=0#section-queries

参考

https://firebase.google.com/docs/admin/setup?authuser=0

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
ユーザーは見つかりませんでした