firestore_update_rest.js
#! /usr/bin/node
// ---------------------------------------------------------------// firestore_update_rest.js//// Feb/01/2020//// ---------------------------------------------------------------varget_token=require('./get_token.js')// ---------------------------------------------------------------functionpatch_proc(token,url,str_json){constargs={data:str_json,headers:{"Authorization":"Bearer "+token}}try{client.patch(url,args,function(data,response){console.log(data)})console.error("*** 終了 ***")}catch(error){console.error("*** error *** from client.get ***")console.error(error)}}// ---------------------------------------------------------------console.error("*** 開始 ***")constkey_in=process.argv[2]constpopulation_in=process.argv[3]console.log(key_in+"\t"+population_in)consttoday=newDate()varddx=(1900+today.getYear())+"-"+(today.getMonth()+1)ddx+="-"+today.getDate()+"T00:00:00.0Z"console.log(ddx)consttoken=get_token.get_token_proc()constproject='project-jan25-2020'consturl="https://firestore.googleapis.com/v1/projects/"+project+"/databases/(default)/documents/cities/"+key_invarClient=require('node-rest-client').Clientvarclient=newClient()varargs={headers:{"Authorization":"Bearer "+token}}client.get(url,args,function(data,response){console.log(data.fields)data.fields.population.integerValue=population_indata.fields.date_mod.timestampValue=ddxconststr_json=JSON.stringify(data)patch_proc(token,url,str_json)})// ---------------------------------------------------------------
get_token.js はこちら
Node.js の Rest で Cloud Firestore のデータを作成 (Create)
実行コマンド
export NODE_PATH=/usr/lib/node_modules
export GOOGLE_APPLICATION_CREDENTIALS="***.json"
./firestore_update_rest.js t0921 8942700