{"id":5586,"date":"2019-04-10T09:43:51","date_gmt":"2019-04-10T00:43:51","guid":{"rendered":"https:\/\/www.lancard.com\/blog\/?p=5586"},"modified":"2025-03-12T11:25:43","modified_gmt":"2025-03-12T02:25:43","slug":"firestore_servertimestamp","status":"publish","type":"post","link":"https:\/\/www.lancard.com\/blog\/2019\/04\/10\/firestore_servertimestamp\/","title":{"rendered":"Firestore\u306eServerTimestamp\u3055\u3093"},"content":{"rendered":"<p>\u6709\u80fd\uff01<\/p>\n<p>\u30b5\u30fc\u30d0\u30fc\u5074\u3067\u63a1\u6642\u3059\u308b\u306e\u3067\u96c6\u8a08\u5bfe\u8c61\u306e\u62bd\u51fa\u306a\u3069\u306b\u5b89\u5fc3\u3057\u3066\u4f7f\u7528\u3067\u304d\u307e\u3059\u3002<br \/>\n\u3053\u308c\u306e\u304a\u304b\u3052\u3067\u5fc5\u8981\u306a\u30c7\u30fc\u30bf\u53d6\u5f97\u6570\u3092\u5927\u5e45\u306b\u524a\u6e1b\u3067\u304d\u3066\u3044\u307e\u3059\u3002<br \/>\nFirestore\u3060\u3068\u30c7\u30fc\u30bf\u53d6\u5f97\u6570\u306f\u901f\u5ea6\u3084\u52b9\u7387\u3060\u3051\u3067\u306a\u304f\u8ab2\u91d1\u306b\u76f4\u7d50\u3057\u307e\u3059\u3057\u306d\u3002<\/p>\n<p>\u7ba1\u7406\u753b\u9762\u3067\u30c7\u30fc\u30bf\u3092\u6642\u7cfb\u5217\u306b\u4e26\u3079\u305f\u308a\u3059\u308b\u306e\u306b\u3082\u4fbf\u5229\u306a\u306e\u3067\u30c9\u30ad\u30e5\u30e1\u30f3\u30c8\u767b\u9332\u6642\u306b\u306f\u5fc5\u305a\u5165\u308c\u3066\u304a\u3044\u305f\u307b\u3046\u304c\u826f\u3044\u304f\u3089\u3044\u306e\u6709\u80fd\u3055\u3067\u3059\u3002<\/p>\n<p>Web\u3067\u306f<\/p>\n<pre><code class=\"Javascript\">import firebase from '~\/plugins\/firebase'\n\npayload.createdAt = firebase.firestore.FieldValue.serverTimestamp()\nawait db.collection('\u30b3\u30ec\u30af\u30b7\u30e7\u30f3\u540d').add(payload)\n<\/code><\/pre>\n<p><!--more--><\/p>\n<p>(Functions\u306a\u3069)node.js\u3067firebase-admin\u3092\u4f7f\u7528\u3059\u308b\u5834\u5408\u306f<\/p>\n<pre><code class=\"Javascript\">const admin = require(\"firebase-admin\");\n\nconst serviceAccount = require(\"\u30b5\u30fc\u30d3\u30b9\u30a2\u30ab\u30a6\u30f3\u30c8\u306e\u9375.json\");\nadmin.initializeApp({\n  credential: admin.credential.cert(serviceAccount)\n});\n\nadmin.firestore().collection(\"\u30b3\u30ec\u30af\u30b7\u30e7\u30f3\u540d\").doc(\"\u30c9\u30ad\u30e5\u30e1\u30f3\u30c8\u306e\u30ad\u30fc\").set({\n  updatedAt: admin.firestore.FieldValue.serverTimestamp()\n});\n<\/code><\/pre>\n<p>\u3068\u3044\u3063\u305f\u611f\u3058\u3067\u30b5\u30fc\u30d0\u30fc\u5074\u3067Timestamp\u3092\u6295\u5165\u3059\u308b\u3088\u3046\u306b\u6307\u793a\u3067\u304d\u307e\u3059\u3002<\/p>\n<p>\u3067\u3082\u6ce8\u610f\u70b9\u304c\u3002<\/p>\n<p><strong>\u767b\u9332\u76f4\u5f8c\u306b\u30c9\u30ad\u30e5\u30e1\u30f3\u30c8\u3092\u53d6\u5f97\u3059\u308b\u3068Timestamp\u304c\u5165\u3063\u3066\u306a\u3044\u72b6\u614b\u3067\u8fd4\u3063\u3066\u304d\u307e\u3059<\/strong>(2019\u5e743\u6708)\u3002await\u7b49\u3067\u5b8c\u4e86\u3092\u307e\u3063\u3066\u3082\u3001\u3067\u3059\u3002<br \/>\n\u305f\u3060\u3057\u3053\u308c\u306f\u30d5\u30ed\u30f3\u30c8\u30a8\u30f3\u30c9\u304b\u3089\u306e\u53d6\u5f97\u306e\u8a71\u3067onCreate\u3067\u8d77\u52d5\u3055\u308c\u305fFunctions\u3067\u306f\u3055\u3059\u304c\u306b\u3061\u3083\u3093\u3068\u5165\u3063\u3066\u3044\u308b\u3088\u3046\u3067\u3059\u3002<\/p>\n<p>\u30d5\u30ed\u30f3\u30c8\u30a8\u30f3\u30c9\u3067\u767b\u9332\u76f4\u5f8c\u306b\u30c7\u30fc\u30bf\u3092\u53d6\u5f97\u3057(Server)Timestamp\u3092\u5229\u7528\u3059\u308b\u306b\u306f\u63a1\u6642\u3055\u308c\u308b\u306e\u3092\u5f85\u3064\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\u30bf\u30a4\u30df\u30f3\u30b0\u306e\u554f\u984c\u3063\u3066\u3053\u3068\u3067\u3059(\uff1b\u00b4\u0414\uff40)<\/p>\n<p>\u79c1\u306e\u5834\u5408\u306f\u64cd\u4f5c\u30ed\u30b0\u8a18\u9332\u306e\u305f\u3081\u306e\u5909\u66f4\u5f8c\u306e\u30c9\u30ad\u30e5\u30e1\u30f3\u30c8\u53d6\u5f97\u3067\u3057\u305f\u306e\u3067\u3001\u30ed\u30b0\u767b\u9332\u3068\u3044\u3046\u51e6\u7406\u306e\u6027\u8cea\u4e0a\u30e6\u30fc\u30b6\u30fc\u304b\u3089\u898b\u3048\u308b\u5fc5\u8981\u306f\u306a\u3044\u306e\u3067\u975e\u540c\u671f\u306b500ms\u30c7\u30a3\u30ec\u30a4\u3057\u3066\u53d6\u5f97\u30fb\u767b\u9332\u51e6\u7406\u3092\u884c\u3046\u3088\u3046\u306b\u3057\u307e\u3057\u305f(lodash\u306edelay\u3092\u5229\u7528)<\/p>\n<p>\u306a\u3093\u304b\u8a18\u4e8b\u66f8\u3044\u3066\u3066\u3082\u30db\u30f3\u30c8\u304b\u3088\u3063\u3066\u611f\u3058\u3067\u3059\u304c\u3002<\/p>\n<a class=\"synved-social-button synved-social-button-share synved-social-size-24 synved-social-resolution-single synved-social-provider-facebook nolightbox\" data-provider=\"facebook\" target=\"_blank\" rel=\"nofollow\" title=\"Share on Facebook\" href=\"https:\/\/www.facebook.com\/sharer.php?u=https%3A%2F%2Fwww.lancard.com%2Fblog%2Fwp-json%2Fwp%2Fv2%2Fposts%2F5586&amp;t=Firestore%E3%81%AEServerTimestamp%E3%81%95%E3%82%93&amp;s=100&amp;p[url]=https%3A%2F%2Fwww.lancard.com%2Fblog%2Fwp-json%2Fwp%2Fv2%2Fposts%2F5586&amp;p[images][0]=&amp;p[title]=Firestore%E3%81%AEServerTimestamp%E3%81%95%E3%82%93\" style=\"font-size: 0px;width:24px;height:24px;margin:0;margin-bottom:5px;margin-right:5px\"><img loading=\"lazy\" decoding=\"async\" alt=\"Facebook\" title=\"Share on Facebook\" class=\"synved-share-image synved-social-image synved-social-image-share\" width=\"24\" height=\"24\" style=\"display: inline;width:24px;height:24px;margin: 0;padding: 0;border: none;box-shadow: none\" src=\"https:\/\/www.lancard.com\/blog\/wp-content\/plugins\/social-media-feather\/synved-social\/image\/social\/regular\/48x48\/facebook.png\" \/><\/a><a class=\"synved-social-button synved-social-button-share synved-social-size-24 synved-social-resolution-single synved-social-provider-twitter nolightbox\" data-provider=\"twitter\" target=\"_blank\" rel=\"nofollow\" title=\"Share on Twitter\" href=\"http:\/\/twitter.com\/share?url=https%3A%2F%2Fwww.lancard.com%2Fblog%2Fwp-json%2Fwp%2Fv2%2Fposts%2F5586&amp;text=Firestore%E3%81%AEServerTimestamp%E3%81%95%E3%82%93\" style=\"font-size: 0px;width:24px;height:24px;margin:0;margin-bottom:5px;margin-right:5px\"><img loading=\"lazy\" decoding=\"async\" alt=\"twitter\" title=\"Share on Twitter\" class=\"synved-share-image synved-social-image synved-social-image-share\" width=\"24\" height=\"24\" style=\"display: inline;width:24px;height:24px;margin: 0;padding: 0;border: none;box-shadow: none\" src=\"https:\/\/www.lancard.com\/blog\/wp-content\/plugins\/social-media-feather\/synved-social\/image\/social\/regular\/48x48\/twitter.png\" \/><\/a><a class=\"synved-social-button synved-social-button-share synved-social-size-24 synved-social-resolution-single synved-social-provider-linkedin nolightbox\" data-provider=\"linkedin\" target=\"_blank\" rel=\"nofollow\" title=\"Share on Linkedin\" href=\"https:\/\/www.linkedin.com\/shareArticle?mini=true&amp;url=https%3A%2F%2Fwww.lancard.com%2Fblog%2Fwp-json%2Fwp%2Fv2%2Fposts%2F5586&amp;title=Firestore%E3%81%AEServerTimestamp%E3%81%95%E3%82%93\" style=\"font-size: 0px;width:24px;height:24px;margin:0;margin-bottom:5px;margin-right:5px\"><img loading=\"lazy\" decoding=\"async\" alt=\"linkedin\" title=\"Share on Linkedin\" class=\"synved-share-image synved-social-image synved-social-image-share\" width=\"24\" height=\"24\" style=\"display: inline;width:24px;height:24px;margin: 0;padding: 0;border: none;box-shadow: none\" src=\"https:\/\/www.lancard.com\/blog\/wp-content\/plugins\/social-media-feather\/synved-social\/image\/social\/regular\/48x48\/linkedin.png\" \/><\/a><a class=\"synved-social-button synved-social-button-share synved-social-size-24 synved-social-resolution-single synved-social-provider-tumblr nolightbox\" data-provider=\"tumblr\" target=\"_blank\" rel=\"nofollow\" title=\"Share on tumblr\" href=\"https:\/\/tumblr.com\/share?s=&amp;v=3&amp;t=Firestore%E3%81%AEServerTimestamp%E3%81%95%E3%82%93&amp;u=https%3A%2F%2Fwww.lancard.com%2Fblog%2Fwp-json%2Fwp%2Fv2%2Fposts%2F5586\" style=\"font-size: 0px;width:24px;height:24px;margin:0;margin-bottom:5px;margin-right:5px\"><img loading=\"lazy\" decoding=\"async\" alt=\"tumblr\" title=\"Share on tumblr\" class=\"synved-share-image synved-social-image synved-social-image-share\" width=\"24\" height=\"24\" style=\"display: inline;width:24px;height:24px;margin: 0;padding: 0;border: none;box-shadow: none\" src=\"https:\/\/www.lancard.com\/blog\/wp-content\/plugins\/social-media-feather\/synved-social\/image\/social\/regular\/48x48\/tumblr.png\" \/><\/a><a class=\"synved-social-button synved-social-button-share synved-social-size-24 synved-social-resolution-single synved-social-provider-mail nolightbox\" data-provider=\"mail\" rel=\"nofollow\" title=\"Share by email\" href=\"mailto:?subject=Firestore%E3%81%AEServerTimestamp%E3%81%95%E3%82%93&amp;body=%E3%82%B7%E3%82%A7%E3%82%A2%E3%81%99%E3%82%8B%EF%BC%9A:%20https%3A%2F%2Fwww.lancard.com%2Fblog%2Fwp-json%2Fwp%2Fv2%2Fposts%2F5586\" style=\"font-size: 0px;width:24px;height:24px;margin:0;margin-bottom:5px\"><img loading=\"lazy\" decoding=\"async\" alt=\"mail\" title=\"Share by email\" class=\"synved-share-image synved-social-image synved-social-image-share\" width=\"24\" height=\"24\" style=\"display: inline;width:24px;height:24px;margin: 0;padding: 0;border: none;box-shadow: none\" src=\"https:\/\/www.lancard.com\/blog\/wp-content\/plugins\/social-media-feather\/synved-social\/image\/social\/regular\/48x48\/mail.png\" \/><\/a>","protected":false},"excerpt":{"rendered":"<p>\u6709\u80fd\uff01 \u30b5\u30fc\u30d0\u30fc\u5074\u3067\u63a1\u6642\u3059\u308b\u306e\u3067\u96c6\u8a08\u5bfe\u8c61\u306e\u62bd\u51fa\u306a\u3069\u306b\u5b89\u5fc3\u3057\u3066\u4f7f\u7528\u3067\u304d\u307e\u3059\u3002 \u3053\u308c\u306e\u304a\u304b\u3052\u3067\u5fc5\u8981\u306a\u30c7\u30fc\u30bf\u53d6\u5f97\u6570\u3092\u5927\u5e45\u306b\u524a\u6e1b\u3067\u304d\u3066\u3044\u307e\u3059\u3002 Firestore\u3060\u3068\u30c7\u30fc\u30bf\u53d6\u5f97\u6570\u306f\u901f\u5ea6\u3084\u52b9\u7387\u3060\u3051\u3067\u306a\u304f\u8ab2\u91d1\u306b\u76f4\u7d50\u3057\u307e\u3059\u3057\u306d\u3002 \u7ba1\u7406 [&hellip;]<\/p>\n","protected":false},"author":12,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[155],"tags":[190,191],"class_list":["post-5586","post","type-post","status-publish","format-standard","hentry","category-javascript","tag-firebase","tag-firestore"],"_links":{"self":[{"href":"https:\/\/www.lancard.com\/blog\/wp-json\/wp\/v2\/posts\/5586","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.lancard.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.lancard.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.lancard.com\/blog\/wp-json\/wp\/v2\/users\/12"}],"replies":[{"embeddable":true,"href":"https:\/\/www.lancard.com\/blog\/wp-json\/wp\/v2\/comments?post=5586"}],"version-history":[{"count":5,"href":"https:\/\/www.lancard.com\/blog\/wp-json\/wp\/v2\/posts\/5586\/revisions"}],"predecessor-version":[{"id":5595,"href":"https:\/\/www.lancard.com\/blog\/wp-json\/wp\/v2\/posts\/5586\/revisions\/5595"}],"wp:attachment":[{"href":"https:\/\/www.lancard.com\/blog\/wp-json\/wp\/v2\/media?parent=5586"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.lancard.com\/blog\/wp-json\/wp\/v2\/categories?post=5586"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.lancard.com\/blog\/wp-json\/wp\/v2\/tags?post=5586"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}