Node.js で Cloud Firestore のデータを更新 (Update)


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

firestore_update.js
#! /usr/bin/node
// ---------------------------------------------------------------
//  firestore_update.js
//
//                  Feb/09/2021
//
// ---------------------------------------------------------------
console.error ("*** 開始 ***")

const key_in=process.argv[2]
const population_in=process.argv[3]

console.log (key_in + "\t" + population_in)


const today = new Date ()
var ddx = (1900 + today.getFullYear ()) + "-" + (today.getMonth () +1)
ddx += "-" + today.getDate ()
console.log(ddx)

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

const db = admin.firestore()

try
    {
    let cityRef = db.collection('cities').doc(key_in)

    cityRef.update({population: population_in,
        date_mod: ddx})
    }
catch (error)
    {
    console.error ("*** error *** update ***")
    console.error (error)
    }

console.error ("*** 終了 ***")


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

実行コマンド

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_update.js t0923 91456200