除了图像之外,注册表单数据未存储在Firebase上>>

问题描述 投票:0回答:2

我正在尝试使用Firebase

react-native中创建注册表单。我已经使用Fetch BlobDocument Picker库获取图像并将upload它添加到firebase 。而且我还试图将用户的名称,电子邮件和密码保存在实时数据库中。但不幸的是,除了firebase storage中的image上载,用户数据不会保存在数据库中。

这是我的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);
      });
  };

这是Firebase控制台的屏幕截图

enter image description here

enter image description here

enter image description here

我正在尝试使用Firebase在react-native中创建注册表单。我使用了Fetch Blob和Document Picker库来获取图像并将其上传到firebase。而且我也在尝试保存用户的...

reactjs firebase react-native firebase-realtime-database react-native-firebase
2个回答
0
投票

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

  1. 转到firebase控制台并打开您的项目。
  2. 转到数据库并搜索“规则”选项卡。

0
投票

我已经解决了这个问题。问题出在这段代码中。

 const downnloadImageURI = imageRef.getDownloadURL().then(url => {
            this.setState(
              {
                imageURI: url,
              },
              () => {
                alert('ImageURI ==> ', this.state.imageURI);
                this.saveUserInfo();
              },
            );
© www.soinside.com 2019 - 2024. All rights reserved.