firestore_read_rest.js
#! /usr/bin/node
// ---------------------------------------------------------------
// firestore_read_rest.js
//
// Feb/01/2020
//
// ---------------------------------------------------------------
var fs = require("fs")
var get_token = require('./get_token.js')
// ---------------------------------------------------------------
function row_parser(doc)
{
// console.log(doc)
const array_aa = doc.name.split("/")
const llx = array_aa.length
const key = array_aa[llx - 1]
const name = doc.fields.name.stringValue
const population = doc.fields.population.integerValue
const date_mod = doc.fields.date_mod.timestampValue
const str_out = key + "\t" + name + "\t" + population + "\t" + date_mod
console.log(str_out)
}
// ---------------------------------------------------------------
console.error ("*** 開始 ***")
const env = process.env
// console.log(env.GOOGLE_APPLICATION_CREDENTIALS)
const json_str = fs.readFileSync (env.GOOGLE_APPLICATION_CREDENTIALS,'utf8')
const dict_aa = JSON.parse (json_str)
const project_id = dict_aa['project_id']
console.log(project_id)
const token = get_token.get_token_proc()
const url="https://firestore.googleapis.com/v1/projects/" + project_id + "/databases/(default)/documents/cities"
var Client = require('node-rest-client').Client
var client = new Client()
var args = {
headers: { "Authorization": "Bearer " + token}
}
try
{
client.get(url, args, function (data, response) {
try
{
data['documents'].forEach((doc) => {
row_parser(doc)
})
}
catch (error)
{
console.error ("*** error *** from forEach ***")
console.error (error)
}
console.error ("*** 終了 ***")
})
}
catch (error)
{
console.error ("*** error *** from client.get ***")
console.error (error)
}
// ---------------------------------------------------------------
get_token.js はこちら
Node.js の Rest で Cloud Firestore のデータを作成 (Create)
実行コマンド
export NODE_PATH=/usr/lib/node_modules
export GOOGLE_APPLICATION_CREDENTIALS="***.json"
./firestore_read_rest.js