繁体   English   中英

如何将日期设置为 firebase?

[英]How to set date to firebase?

我尝试第一次使用 firebase。 我创建了非常开放的规则:

service cloud.firestore {
  match /databases/{database}/documents {
    // Make sure the uid of the requesting user matches name of the user
    // document. The wildcard expression {userId} makes the userId variable
    // available in rules.
    match /users/{userId} {
      allow read, update, delete, create: if 1 == 1;
 match /{document=**} {
        allow read, write, update, delete, create: if 2 == 2;
      }
}
  }
}

然后我想将我的测试消息发送到服务器(我成功安装登录/注册和重置应用程序,现在,我的用户已登录)。

import { useAuthState } from "react-firebase-hooks/auth";
import { auth, db, logout } from "./firebase";
import { query, collection, getDocs, where } from "firebase/firestore";
import { getDatabase, ref, set } from "firebase/database";

     const SaveDateBase = () => {
          e.preventDefault();
  db.collection('Books').set({
            title: "pokemon"
          })
         }
return (<>
(...)
<button type="submit" onSubmit={SaveDateBase}>Save changes</button>
</>

不用说,我用我的一个工作示例创建了“书籍”系列。

你的规则有问题

您的规则允许读取和写入顶级users集合以及其中的所有数据。 但是您的代码尝试写入顶级Books集合。 由于您的规则都没有提到此集合,因此该写入被拒绝。


您的代码中的问题

此外,此代码将不起作用:

db.collection('Books').set({
  title: "pokemon"
})

您正在尝试直接写入Books集合,这不是一个选项(并且应该给您一个非常明确的错误)。 如果要将文档添加Books集合:

db.collection('Books').add({
  title: "pokemon"
})

如果要设置文档,还需要指定文档 ID:

db.collection('Books').doc('MyFirstPokemonBook').add({
  title: "pokemon"
})

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM