1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Node.js で Cloud Firestore のデータを作成 (Create)

Last updated at Posted at 2020-01-25

ライブラリーのインストール

sudo npm install -g firebase-admin
firestore_create.js
#! /usr/bin/node
// ---------------------------------------------------------------
//	firestore_create.js
//
//					Jan/25/2020
//
// ---------------------------------------------------------------
function add_data_proc(db,key,name,population,date_mod)
{
	let tt_ref = db.collection('cities').doc(key)
	tt_ref.set({
		'name': name,
		'population': population,
		'date_mod': date_mod
		})
}

// ---------------------------------------------------------------
console.error ("*** 開始 ***")

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

const db = admin.firestore();

add_data_proc(db,'t0921','宇都宮',81235,'1950-9-24')
add_data_proc(db,'t0922','小山',94235,'1950-2-15')
add_data_proc(db,'t0923','佐野',71395,'1950-5-28')
add_data_proc(db,'t0924','足利',35421,'1950-7-8')
add_data_proc(db,'t0925','日光',61948,'1950-1-11')


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_create.js

次のバージョンで確認しました。

$ node --version
v14.14.0
1
2
0

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
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?