LoginSignup
0
0

More than 3 years have passed since last update.

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

Last updated at Posted at 2020-01-25

こちらで作成したデータを更新します。
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
0
0
3

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