简体   繁体   English

如何为预期创建的文档增加价值? (Firebase + 反应)

[英]How do I add value to a document that is expected to be created? (Firebase + React)

I want to add a field (Username) to a firebase user's document, when i create it.我想在创建它时向 firebase 用户的文档添加一个字段(用户名)。

My logic :我的逻辑:

  1. Fill up form with email,pass & username field as well - WORKS.用电子邮件,通行证和用户名字段填写表格 - 有效。
  2. Take the email & pass and sign up with firebase (createUserWithEmailAndPassword) - WORKS.获取电子邮件并通过并注册 firebase (createUserWithEmailAndPassword) - WORKS。
  3. Right after the document creation (Hopefully) - fire an HTTP Firebase callable that will update userName field from the form - FAILED .在文档创建之后(希望如此) - 触发一个 HTTP Firebase 可调用,它将更新表单中的 userName 字段 - FAILED

Error : Unhandled error { Error: 5 NOT_FOUND: No document to update: projects/yeu/databases/(default)/documents/users/DchYhQNMS12VYqzSwtEZ8UXYhcD3错误:未处理的错误 { 错误:5 NOT_FOUND:没有要更新的文档:projects/yeu/databases/(默认)/documents/users/DchYhQNMS12VYqzSwtEZ8UXYhcD3

I understand that there is no document (not yet) - so my question is how can i make sure that the document will be ready when it's need to be.我知道没有文件(还没有) - 所以我的问题是我如何确保文件在需要时准备好。

CODE:代码:

ReactJS - SignUp event, (using form & OnClick) ReactJS - 注册事件,(使用表单和 OnClick)

export function SignUp() {    

  const handleSignupSubmit = (event) => {
    event.preventDefault();
    const registerForm = document.querySelector(".register");

    // firebase
    const addUserName = firebase.functions().httpsCallable("addUserName");

    // register form
    const email = registerForm.email.value;
    const password = registerForm.password.value;
    const fullName = registerForm.fullName.value;

    // firebase sign up
    firebase
      .auth()
      .createUserWithEmailAndPassword(email, password)
      .then((userAuth) => {
        console.log("registered", userAuth);
        addUserName({
          userName : fullName
        }).then(((doc)=>{
          console.log("UserName Added", doc);
        }))
        .catch((error) => {
          registerForm.reset();
          registerForm.querySelector(".error").textContent = error.message;
        });
      })
      .catch((error) => {
        registerForm.querySelector(".error").textContent = error.message;
      });
  };

Firebase Backend - Auth trigger Firebase 后端 - 身份验证触发器

// auth trigger (new user signup)
exports.newUserSignUp = functions.auth.user().onCreate((user) => {
  // for background triggers you must return a value/promise
  return admin.firestore().collection("users").doc(user.uid).set({
    userName: 'Temp',
    email: user.email,
  });
});

Firebase Backend - HTTP Callable method Firebase 后端 - HTTP 可调用方法

// http callable function (adding a username)
exports.addUserName = functions.https.onCall((data, context) => {
  if (!context.auth) {
    throw new functions.https.HttpsError("unauthenticated");
  }    
  const userRef = admin.firestore().collection("users").doc(context.auth.uid);

  return userRef.get().then((doc) => {
    return userRef.update({
      userName: data.userName
    })
  })
});

Your client app should wait for the document to become available before invoking the callable function.在调用可调用函数之前,您的客户端应用程序应等待文档可用。 The easiest way to do that is to set up a realtime listener for the expected document, then call the function only after the listener gives you a callback indicating that the document is present and has data.最简单的方法是为预期的文档设置一个实时侦听器,然后仅在侦听器给您一个回调指示该文档存在并且有数据后才调用该函数。

The pattern is described in more detail in this blog post . 这篇博文中更详细地描述了该模式。 You should be able to copy what it's doing very closely.您应该能够非常密切地复制它正在做的事情。

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

相关问题 如何检查集合中是否存在文档 (Firebase REACT)? - How do I check if a document exists within a collection (Firebase REACT)? React Native 如何获取 Firebase 中特定文档的文档 ID? - How do I get the document ID of a specific document in Firebase on React Native? 如何将子集合添加到 firebase 中新创建的文档? - How to add a sub collection to a freshly created document in firebase? 如何将 firebase 添加到我的反应项目中? - How do I add firebase to my react project? 如何在 firebase react js 中向经过身份验证的用户添加集合? - How do I add collections to authenticated user in firebase react js? 如何使用React从Firebase检索字段值? - How do I retrieve a field value from Firebase using React? 如何在不弹出的情况下将 WebPack polyfill 添加到使用 React Create App 创建的 React App? - How do I add WebPack polyfill to React App created with React Create App without ejecting? 我如何在反应 typescript 中参考文档? - How do i reference document in react typescript? Mongoose:如何将之后创建的文档中的数据传递到之前创建的文档中? - Mongoose: How do I pass data from a document created afterwards into a document created right before? 反应 firebase 将新文档添加到集合 - react firebase add new document to collection
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM