0
0

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.

Firestoreのsetメソッド基本の使い方

Last updated at Posted at 2020-08-10

1. addメソッドとは

  • データベースに追加するときに使う
  • firesroreのaddメソッドは自動でIDを採番してくれるので単純にデータを渡せば良い
const itemRef = db.collection('items')
const data = {
  name: name,
  price: parseInt(price, 10)
}

return itemRef.add(data)
  .then(() => {
    dispatch(push('/'))
})

2. setメソッドとは

  • idを指定して登録できる
  • 何も指定がないと自動で設定される(addと同じ)
return itemRef.doc().set(data)
  • 事前に割り振られるIDを取得できる
const ref = itemRef.doc();
const id = ref.id;
data.id = id
return itemRef.doc(id).set(data)
  • 変更部分のみmargeできる
return item.Ref.doc(id).set(data, {marge: true})
0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?