{"id":1130,"date":"2024-04-23T21:43:48","date_gmt":"2024-04-23T12:43:48","guid":{"rendered":"https:\/\/shinke1987.net\/?p=1130"},"modified":"2024-04-25T18:54:05","modified_gmt":"2024-04-25T09:54:05","slug":"react%e3%81%a7firestore%e3%82%92%e6%93%8d%e4%bd%9c","status":"publish","type":"post","link":"https:\/\/shinke1987.net\/?p=1130","title":{"rendered":"React\u3067Firestore\u306e\u57fa\u672c\u7684\u306a\u64cd\u4f5c"},"content":{"rendered":"\n<h2 id=\"toc0\" class=\"wp-block-heading\">\u53c2\u8003\u8cc7\u6599<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/firebase.google.com\/docs\/reference\/js\/?hl=ja&amp;authuser=0&amp;_gl=1*15nuuca*_ga*MzI1MzAwNDMyLjE3MTMxNDk3MjM.*_ga_CW55HF8NVT*MTcxMzg0OTQwMi4zLjEuMTcxMzg1MDg4NC42MC4wLjA\">API Reference | Firebase JavaScript API reference<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/firebase.google.com\/docs\/samples\/?hl=ja&amp;authuser=0&amp;_gl=1*15nuuca*_ga*MzI1MzAwNDMyLjE3MTMxNDk3MjM.*_ga_CW55HF8NVT*MTcxMzg0OTQwMi4zLjEuMTcxMzg1MDg4NC42MC4wLjA\">Firebase \u30c9\u30ad\u30e5\u30e1\u30f3\u30c8\uff08\u30b5\u30f3\u30d7\u30eb\uff09<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/firebase.google.com\/docs\/web\/setup?authuser=0&amp;hl=ja\">Firebase \u3092 JavaScript \u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u306b\u8ffd\u52a0\u3059\u308b<\/a><\/li>\n<\/ul>\n\n\n\n<h2 id=\"toc1\" class=\"wp-block-heading\">SDK\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb<\/h2>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\n# npm install firebase@10.11.0\n<\/pre><\/div>\n\n\n<h2 id=\"toc2\" class=\"wp-block-heading\">Firestore\u306e\u57fa\u672c\u7684\u306a\u64cd\u4f5c<\/h2>\n\n\n\n<h3 id=\"toc3\" class=\"wp-block-heading\">\u524d\u63d0<\/h3>\n\n\n\n<p>Firestore\u306f\u30a6\u30a7\u30d6\u30a2\u30d7\u30ea\u7528\u3068\u3057\u3066\u8a2d\u5b9a\u3057\u305f\u3002<\/p>\n\n\n\n<p>\u4e0b\u8a18\u30b3\u30fc\u30c9\u306f\u30c7\u30d5\u30a9\u30eb\u30c8\u306eFirestore\u30eb\u30fc\u30eb\u3067\u5b9f\u884c\u3057\u305f\u3002<\/p>\n\n\n\n<h3 id=\"toc4\" class=\"wp-block-heading\">\u30c7\u30fc\u30bf\u69cb\u9020<\/h3>\n\n\n\n<p>\u30c7\u30fc\u30bf\u306f\u30c9\u30ad\u30e5\u30e1\u30f3\u30c8\u306b\u4fdd\u5b58\u3057\u3001\u30c9\u30ad\u30e5\u30e1\u30f3\u30c8\u306f\u30b3\u30ec\u30af\u30b7\u30e7\u30f3\u306b\u307e\u3068\u3081\u3089\u308c\u308b\u3002<\/p>\n\n\n\n<h3 id=\"toc5\" class=\"wp-block-heading\">\u30c7\u30fc\u30bf\u8ffd\u52a0<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\nimport React from &quot;react&quot;;\nimport ReactDOM from &#039;react-dom\/client&#039;;\nimport { initializeApp } from &#039;firebase\/app&#039;;\nimport {\n    getFirestore,\n    doc,\n    setDoc\n} from &#039;firebase\/firestore&#039;;\n\nconst firebaseConfig = {\n    \/\/ apiKey\u7b49\u3092\u8a2d\u5b9a\u3059\u308bFirebase\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u56fa\u6709\u306e\u60c5\u5831\u3002\n};\n\nconst app = initializeApp(firebaseConfig);\nconst db = getFirestore(app);\n\n\/\/ users\u30b3\u30ec\u30af\u30b7\u30e7\u30f3\u306edocument_name\u30c9\u30ad\u30e5\u30e1\u30f3\u30c8\u3078\u306e\u53c2\u7167\u3002\nconst fb_document = doc(db, &#039;users&#039;, &#039;document_name&#039;);\n\n\/\/ \u8ffd\u52a0\u3059\u308b\u30c7\u30fc\u30bf\u3002\nconst data = {\n    first: &#039;\u30d5\u30a1\u30fc\u30b9\u30c8&#039;,\n    last: &#039;\u30e9\u30b9\u30c8&#039;,\n    born: 2024\n};\n\n\/\/ \u30c7\u30fc\u30bf\u8ffd\u52a0\u3002\nawait setDoc(fb_document, data);\n\n\/\/ users\u3068\u3044\u3046\u30b3\u30ec\u30af\u30b7\u30e7\u30f3\u306e\u3001\n\/\/ document_name\u3068\u3044\u3046\u30c9\u30ad\u30e5\u30e1\u30f3\u30c8\u306b\u3001\n\/\/ data\u306e\u5185\u5bb9\u304c\u4fdd\u5b58\u3055\u308c\u3066\u3044\u308b\u3053\u3068\u3092\u78ba\u8a8d\u3059\u308c\u3070\u826f\u3044\u3002\n\n\/\/ \u8ffd\u52a0\u3067\u304d\u306a\u304b\u3063\u305f\u5834\u5408\u306e\u4f8b\u5916\u3082catch\u3057\u305f\u65b9\u304c\u826f\u3044\u3002\n\nReactDOM.createRoot(document.getElementById(&#039;root&#039;)!).render(\n    &lt;React.StrictMode&gt;\n    &lt;\/React.StrictMode&gt;\n);\n<\/pre><\/div>\n\n\n<h3 id=\"toc6\" class=\"wp-block-heading\">\u30c7\u30fc\u30bf\u53c2\u7167<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\nimport React from &quot;react&quot;;\nimport ReactDOM from &#039;react-dom\/client&#039;;\nimport { initializeApp } from &#039;firebase\/app&#039;;\nimport {\n    getFirestore,\n    collection,\n    getDocs,\n    doc,\n    getDoc,\n    query,\n    where\n} from &#039;firebase\/firestore&#039;;\n\nconst firebaseConfig = {\n    \/\/ apiKey\u7b49\u3092\u8a2d\u5b9a\u3059\u308bFirebase\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u56fa\u6709\u306e\u60c5\u5831\u3002\n};\n\nconst app = initializeApp(firebaseConfig);\nconst db = getFirestore(app);\n\n\/\/===== users\u30b3\u30ec\u30af\u30b7\u30e7\u30f3\u306e\u30c9\u30ad\u30e5\u30e1\u30f3\u30c8\u5168\u3066\u8aad\u307f\u8fbc\u307f =====\n\/\/ users\u30b3\u30ec\u30af\u30b7\u30e7\u30f3\u3078\u306e\u53c2\u7167\u3002\nconst fb_collection = collection(db, &#039;users&#039;);\n\n\/\/ users\u30b3\u30ec\u30af\u30b7\u30e7\u30f3\u306e\u5168\u30c9\u30ad\u30e5\u30e1\u30f3\u30c8\u3078\u306e\u53c2\u7167\u3002\nconst querySnapShot = await getDocs(fb_collection);\n\nquerySnapShot.forEach((doc) =&gt; {\n    console.log(&quot;\u30c9\u30ad\u30e5\u30e1\u30f3\u30c8\u8b58\u5225\u5b50 = &quot;, doc.id);\n    console.log(&quot;\u30c7\u30fc\u30bf = &quot;, doc.data());\n    console.log(&quot;first = &quot;, doc.data().first);\n});\n\n\/\/===== users\u30b3\u30ec\u30af\u30b7\u30e7\u30f3\u306e\u6307\u5b9a\u3057\u305f\u30c9\u30ad\u30e5\u30e1\u30f3\u30c8\u3092\u8aad\u307f\u8fbc\u307f =====\n\/\/ users\u30b3\u30ec\u30af\u30b7\u30e7\u30f3\u306edocument_name\u30c9\u30ad\u30e5\u30e1\u30f3\u30c8\u3078\u306e\u53c2\u7167\u3002\nconst fb_document = doc(db, &#039;users&#039;, &#039;document_name&#039;);\n\n\/\/ users\u30b3\u30ec\u30af\u30b7\u30e7\u30f3\u306edocument_name\u30c9\u30ad\u30e5\u30e1\u30f3\u30c8\u306e\u30c7\u30fc\u30bf\u53d6\u5f97\u3002\nconst fb_doc = await getDoc(fb_document);\n\nconsole.log(&quot;\u30c9\u30ad\u30e5\u30e1\u30f3\u30c8\u8b58\u5225\u5b50 = &quot;, fb_doc.id);\nconsole.log(&quot;\u30c7\u30fc\u30bf = &quot;, fb_doc.data());\nconsole.log(&quot;first = &quot;, fb_doc.data().first);\n\n\/\/===== \u5358\u7d14\u306a\u30af\u30a8\u30ea\u3067\u6761\u4ef6\u3092\u6307\u5b9a\u3057\u3066\u8aad\u307f\u8fbc\u307f =====\n\/\/ users\u30b3\u30ec\u30af\u30b7\u30e7\u30f3\u3078\u306e\u53c2\u7167\u3002\nconst fb_collection = collection(db, &#039;users&#039;);\n\n\/\/ \u30af\u30a8\u30ea\u5236\u7d04\u3092\u8a2d\u5b9a\u3002\nconst fb_where = where(&#039;born&#039;, &#039;==&#039;, 2024);\n\n\/\/ \u30af\u30a8\u30ea\u8a2d\u5b9a\u3002\nconst fb_query = query(fb_collection, fb_where);\n\n\/\/ \u30c7\u30fc\u30bf\u53d6\u5f97\u3002\nconst fb_docs = await getDocs(fb_query);\n\nfb_docs.forEach((fb_doc) =&gt; {\n    console.log(&quot;\u30c9\u30ad\u30e5\u30e1\u30f3\u30c8\u8b58\u5225\u5b50 = &quot;, fb_doc.id);\n    console.log(&quot;\u30c7\u30fc\u30bf = &quot;, fb_doc.data());\n    console.log(&quot;first = &quot;, fb_doc.data().first);\n});\n\nReactDOM.createRoot(document.getElementById(&#039;root&#039;)!).render(\n    &lt;React.StrictMode&gt;\n    &lt;\/React.StrictMode&gt;\n);\n<\/pre><\/div>\n\n\n<h2 id=\"toc7\" class=\"wp-block-heading\">\u30c7\u30fc\u30bf\u66f4\u65b0<\/h2>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\nimport React from &quot;react&quot;;\nimport ReactDOM from &#039;react-dom\/client&#039;;\nimport { initializeApp } from &#039;firebase\/app&#039;;\nimport {\n    getFirestore,\n    doc,\n    updateDoc\n} from &#039;firebase\/firestore&#039;;\n\nconst firebaseConfig = {\n    \/\/ apiKey\u7b49\u3092\u8a2d\u5b9a\u3059\u308bFirebase\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u56fa\u6709\u306e\u60c5\u5831\u3002\n};\n\nconst app = initializeApp(firebaseConfig);\nconst db = getFirestore(app);\n\n\/\/ \u30c9\u30ad\u30e5\u30e1\u30f3\u30c8\u306e\u53c2\u7167\u3002\nconst fb_doc = doc(db, &#039;users&#039;, &#039;document_name&#039;);\n\n\/\/ \u66f4\u65b0\u5f8c\u306e\u30c7\u30fc\u30bf\u3092\u6e96\u5099\u3002\nconst data = {\n    born: 3024\n};\n\n\/\/ \u66f4\u65b0\u3002\nawait updateDoc(fb_doc, data);\n\nReactDOM.createRoot(document.getElementById(&#039;root&#039;)!).render(\n    &lt;React.StrictMode&gt;\n    &lt;\/React.StrictMode&gt;\n);\n<\/pre><\/div>\n\n\n<h2 id=\"toc8\" class=\"wp-block-heading\">\u30c7\u30fc\u30bf\u524a\u9664<\/h2>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\nimport React from &quot;react&quot;;\nimport ReactDOM from &#039;react-dom\/client&#039;;\nimport { initializeApp } from &#039;firebase\/app&#039;;\nimport {\n    getFirestore,\n    doc,\n    updateDoc\n} from &#039;firebase\/firestore&#039;;\n\nconst firebaseConfig = {\n    \/\/ apiKey\u7b49\u3092\u8a2d\u5b9a\u3059\u308bFirebase\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u56fa\u6709\u306e\u60c5\u5831\u3002\n};\n\nconst app = initializeApp(firebaseConfig);\nconst db = getFirestore(app);\n\n\/\/ \u30c9\u30ad\u30e5\u30e1\u30f3\u30c8\u306e\u53c2\u7167\u3002\nconst fb_doc = doc(db, &#039;users&#039;, &#039;document_name&#039;);\n\n\/\/ \u524a\u9664\u3002\nawait deleteDoc(fb_doc);\n\nReactDOM.createRoot(document.getElementById(&#039;root&#039;)!).render(\n    &lt;React.StrictMode&gt;\n    &lt;\/React.StrictMode&gt;\n);\n<\/pre><\/div>","protected":false},"excerpt":{"rendered":"<p>\u53c2\u8003\u8cc7\u6599 SDK\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb Firestore\u306e\u57fa\u672c\u7684\u306a\u64cd\u4f5c \u524d\u63d0 Firestore\u306f\u30a6\u30a7\u30d6\u30a2\u30d7\u30ea\u7528\u3068\u3057\u3066\u8a2d\u5b9a\u3057\u305f\u3002 \u4e0b\u8a18\u30b3\u30fc\u30c9\u306f\u30c7\u30d5\u30a9\u30eb\u30c8\u306eFirestore\u30eb\u30fc\u30eb\u3067\u5b9f\u884c\u3057\u305f\u3002 \u30c7\u30fc\u30bf\u69cb\u9020 \u30c7\u30fc\u30bf\u306f\u30c9\u30ad\u30e5\u30e1\u30f3\u30c8\u306b\u4fdd [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[104],"tags":[110,111,105],"class_list":["post-1130","post","type-post","status-publish","format-standard","hentry","category-react","tag-firebase","tag-firestore","tag-react"],"_links":{"self":[{"href":"https:\/\/shinke1987.net\/index.php?rest_route=\/wp\/v2\/posts\/1130","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/shinke1987.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/shinke1987.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/shinke1987.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/shinke1987.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1130"}],"version-history":[{"count":3,"href":"https:\/\/shinke1987.net\/index.php?rest_route=\/wp\/v2\/posts\/1130\/revisions"}],"predecessor-version":[{"id":1134,"href":"https:\/\/shinke1987.net\/index.php?rest_route=\/wp\/v2\/posts\/1130\/revisions\/1134"}],"wp:attachment":[{"href":"https:\/\/shinke1987.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1130"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/shinke1987.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1130"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/shinke1987.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1130"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}