firestore_read_rest.js
#! /usr/bin/node
// ---------------------------------------------------------------// firestore_read_rest.js//// Feb/01/2020//// ---------------------------------------------------------------varget_token=require('./get_token.js')// ---------------------------------------------------------------functionrow_parser(doc){// console.log(doc)constarray_aa=doc.name.split("/")constllx=array_aa.lengthconstkey=array_aa[llx-1]constname=doc.fields.name.stringValueconstpopulation=doc.fields.population.integerValueconstdate_mod=doc.fields.date_mod.timestampValueconststr_out=key+"\t"+name+"\t"+population+"\t"+date_modconsole.log(str_out)}// ---------------------------------------------------------------console.error("*** 開始 ***")consttoken=get_token.get_token_proc()constproject='project-jan25-2020'consturl="https://firestore.googleapis.com/v1/projects/"+project+"/databases/(default)/documents/cities"varClient=require('node-rest-client').Clientvarclient=newClient()varargs={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