LoginSignup
0
0

More than 3 years have passed since last update.

Node.js で Cloud Firestore のデータを読む (Read)

Last updated at Posted at 2020-01-25

こちらで作成したデータを読みます。
Node.js で Cloud Firestore のデータを作成 (Create)

firestore_read.js
#! /usr/bin/node
// ---------------------------------------------------------------
//  firestore_read.js
//
//                  Jan/25/2020
//
// ---------------------------------------------------------------
console.error ("*** 開始 ***")

const admin = require('firebase-admin')
admin.initializeApp({
  credential: admin.credential.applicationDefault()
})

const db = admin.firestore()


db.collection('cities').get()
  .then((snapshot) => {
    snapshot.forEach((doc) => {
//  console.log(doc.id, '=>', doc.data())
    var unit_aa = doc.data()
    var out_str = doc.id + "\t"
    out_str += unit_aa["name"] + "\t"
    out_str += unit_aa["population"] + "\t"
    out_str += unit_aa["date_mod"]
    console.log (out_str)
    })
console.error ("*** 終了 ***")
  })
  .catch((err) => {
    console.log('Error getting documents', err);
  })


// ---------------------------------------------------------------

実行コマンド

export NODE_PATH=/usr/lib/node_modules
project_id="project-dec16-2020"
echo $project_id
#
export GOOGLE_CLOUD_PROJECT=$project_id
export GOOGLE_APPLICATION_CREDENTIALS="***.json"
./firestore_read.js

次のバージョンで確認しました。

$ node --version
v14.14.0
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0