Reactjs 除图像外,注册表单数据未存储在firebase上

Reactjs 除图像外,注册表单数据未存储在firebase上,reactjs,firebase,react-native,firebase-realtime-database,react-native-firebase,Reactjs,Firebase,React Native,Firebase Realtime Database,React Native Firebase,我正在尝试使用Firebase在react native中创建注册表单。我使用Fetch Blob和Document Picker库获取图像并将其上载到Firebase。我还试图在实时数据库中保存用户的姓名、电子邮件和密码。但不幸的是,用户数据不会保存在数据库中,除非图像上载到firebase存储中 这是我的Firebase身份验证代码 handleSignupOnPress = () => { const {image, email, password} = this.stat

我正在尝试使用Firebasereact native中创建注册表单。我使用
Fetch Blob
Document Picker
库获取图像并将其上载到Firebase。我还试图在实时数据库中保存用户的姓名、电子邮件和密码。但不幸的是,用户数据不会保存在数据库中,除非图像上载到firebase存储中

这是我的Firebase身份验证代码

 handleSignupOnPress = () => {
    const {image, email, password} = this.state;
    let validation = this.validateData();
    console.warn(validation);
    if (validation == true) {
      this.toggleLoading();
      firebaseService
        .auth()
        .createUserWithEmailAndPassword(email, password)
        .then(() => {
          // console.warn("User SignUp Successfully");
          this.uploadImage(image);
        })
        .catch(error => {
          this.toggleLoading();
          var errorCode = error.code;
          var errorMessage = error.message;
          alert(errorMessage);
          // console.warn("ERROR => ", errorCode, errorMessage);
        });
    }
  };
 // First Uploading image and download Image URI then call saveUserToDB()...
  uploadImage(uri, mime = 'image/jpeg') {
    return new Promise((resolve, reject) => {
      const uploadUri =
        Platform.OS === 'ios' ? uri.replace('file://', '') : uri;
      let uploadBlob = '';

      const imageRef = firebaseService
        .storage()
        .ref('images')
        .child(uuid.v4());

      fs.readFile(uploadUri, 'base64')
        .then(data => {
          return Blob.build(data, {type: `${mime};BASE64`});
        })
        .then(blob => {
          uploadBlob = blob;
          return imageRef.put(blob, {contentType: mime});
        })
        .then(() => {
          uploadBlob.close();

          const downnloadImageURI = imageRef.getDownloadURL().then(url => {
            this.setState(
              {
                imageURI: url,
              },
              () => {
                alert('ImageURI ==> ', this.state.imageURI);
                this.saveUserInfo();
              },
            );
          });
          return downnloadImageURI;
        })
        .then(url => {
          resolve(url);
        })
        .catch(error => {
          this.toggleLoading();
          reject(error);
        });
    });
  }
saveUserInfo = () => {
    const {userName, email, password, imageURI} = this.state;
    const {navigate} = this.props.navigation;
    const uid = firebaseService.auth().currentUser.uid;
    const params = {
      image: imageURI,
      username: userName,
      email: email,
      password: password,
    };
    //firebaseService.database().ref('/Users').push(params)
    firebaseService
      .database()
      .ref('/Users')
      .child(uid)
      .set(params)
      .then(res => {
        this.toggleLoading();
        navigate('Login');
      })
      .catch(err => {
        alert(err);
      });
  };
这是图像上传代码

 handleSignupOnPress = () => {
    const {image, email, password} = this.state;
    let validation = this.validateData();
    console.warn(validation);
    if (validation == true) {
      this.toggleLoading();
      firebaseService
        .auth()
        .createUserWithEmailAndPassword(email, password)
        .then(() => {
          // console.warn("User SignUp Successfully");
          this.uploadImage(image);
        })
        .catch(error => {
          this.toggleLoading();
          var errorCode = error.code;
          var errorMessage = error.message;
          alert(errorMessage);
          // console.warn("ERROR => ", errorCode, errorMessage);
        });
    }
  };
 // First Uploading image and download Image URI then call saveUserToDB()...
  uploadImage(uri, mime = 'image/jpeg') {
    return new Promise((resolve, reject) => {
      const uploadUri =
        Platform.OS === 'ios' ? uri.replace('file://', '') : uri;
      let uploadBlob = '';

      const imageRef = firebaseService
        .storage()
        .ref('images')
        .child(uuid.v4());

      fs.readFile(uploadUri, 'base64')
        .then(data => {
          return Blob.build(data, {type: `${mime};BASE64`});
        })
        .then(blob => {
          uploadBlob = blob;
          return imageRef.put(blob, {contentType: mime});
        })
        .then(() => {
          uploadBlob.close();

          const downnloadImageURI = imageRef.getDownloadURL().then(url => {
            this.setState(
              {
                imageURI: url,
              },
              () => {
                alert('ImageURI ==> ', this.state.imageURI);
                this.saveUserInfo();
              },
            );
          });
          return downnloadImageURI;
        })
        .then(url => {
          resolve(url);
        })
        .catch(error => {
          this.toggleLoading();
          reject(error);
        });
    });
  }
saveUserInfo = () => {
    const {userName, email, password, imageURI} = this.state;
    const {navigate} = this.props.navigation;
    const uid = firebaseService.auth().currentUser.uid;
    const params = {
      image: imageURI,
      username: userName,
      email: email,
      password: password,
    };
    //firebaseService.database().ref('/Users').push(params)
    firebaseService
      .database()
      .ref('/Users')
      .child(uid)
      .set(params)
      .then(res => {
        this.toggleLoading();
        navigate('Login');
      })
      .catch(err => {
        alert(err);
      });
  };
以下是保存用户数据的代码

 handleSignupOnPress = () => {
    const {image, email, password} = this.state;
    let validation = this.validateData();
    console.warn(validation);
    if (validation == true) {
      this.toggleLoading();
      firebaseService
        .auth()
        .createUserWithEmailAndPassword(email, password)
        .then(() => {
          // console.warn("User SignUp Successfully");
          this.uploadImage(image);
        })
        .catch(error => {
          this.toggleLoading();
          var errorCode = error.code;
          var errorMessage = error.message;
          alert(errorMessage);
          // console.warn("ERROR => ", errorCode, errorMessage);
        });
    }
  };
 // First Uploading image and download Image URI then call saveUserToDB()...
  uploadImage(uri, mime = 'image/jpeg') {
    return new Promise((resolve, reject) => {
      const uploadUri =
        Platform.OS === 'ios' ? uri.replace('file://', '') : uri;
      let uploadBlob = '';

      const imageRef = firebaseService
        .storage()
        .ref('images')
        .child(uuid.v4());

      fs.readFile(uploadUri, 'base64')
        .then(data => {
          return Blob.build(data, {type: `${mime};BASE64`});
        })
        .then(blob => {
          uploadBlob = blob;
          return imageRef.put(blob, {contentType: mime});
        })
        .then(() => {
          uploadBlob.close();

          const downnloadImageURI = imageRef.getDownloadURL().then(url => {
            this.setState(
              {
                imageURI: url,
              },
              () => {
                alert('ImageURI ==> ', this.state.imageURI);
                this.saveUserInfo();
              },
            );
          });
          return downnloadImageURI;
        })
        .then(url => {
          resolve(url);
        })
        .catch(error => {
          this.toggleLoading();
          reject(error);
        });
    });
  }
saveUserInfo = () => {
    const {userName, email, password, imageURI} = this.state;
    const {navigate} = this.props.navigation;
    const uid = firebaseService.auth().currentUser.uid;
    const params = {
      image: imageURI,
      username: userName,
      email: email,
      password: password,
    };
    //firebaseService.database().ref('/Users').push(params)
    firebaseService
      .database()
      .ref('/Users')
      .child(uid)
      .set(params)
      .then(res => {
        this.toggleLoading();
        navigate('Login');
      })
      .catch(err => {
        alert(err);
      });
  };
以下是Firebase控制台的屏幕截图

数据库中的“规则”是否被授予“写入”权限

  • 转到firebase控制台并打开您的项目
  • 转到数据库并搜索“规则”选项卡
  • 检查规则是否设置如下
  • { /*访问以了解有关安全规则的更多信息*/ “规则”:{ “.read”:正确, “.write”:正确 }
    }我已经解决了这个问题。问题就在这段代码中

     const downnloadImageURI = imageRef.getDownloadURL().then(url => {
                this.setState(
                  {
                    imageURI: url,
                  },
                  () => {
                    alert('ImageURI ==> ', this.state.imageURI);
                    this.saveUserInfo();
                  },
                );
    
    设置状态不工作,并且未触发calback。 我就是这样做的

      const downnloadImageURI = imageRef.getDownloadURL().then(url => {
                   this.saveUserInfo(url)
                    );}
    

    读写规则都是正确的。如果我对所有图片上传代码进行注释,并且只想保存用户的电子邮件密码名称等,那么它就保存了,否则就不仅仅是图片了。它应该可以工作。如果仍然不起作用,那么出现错误的可能性很高,试着找到它。