实施“ react-native-fetch-blob”以在Firebase Storage中上传图像后,我遇到了这个问题。
我尝试从https://github.com/CodeLinkIO/Firebase-Image-Upload-React-Native运行代码。我的代码如下:
uploadImage(uri, mime = "image/jpg", userID) {
return new Promise((resolve, reject) => {
const uploadUri = uri;
let uploadBlob = null;
const imageRef = firebase
.storage()
.ref("images")
.child(userID);
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();
return imageRef.getDownloadURL();
})
.then(url => {
resolve(url);
})
.catch(error => {
reject(error);
});
});
}
但是我总是收到错误提示:
不变违反对象无效,不能作为react子对象(找到:错误无效参数“ path”)
有关此问题的任何帮助吗?
编辑 这是其他渲染代码。
this.state.isLoading ?
<Spinner style={{flex:1, alignSelf: 'center'}} color='blue' />
:
<Container>
<Content style={{margin:20}}>
<Item>
<Icon type="FontAwesome" active name='user' style={{ color :'#c5aa6a'}} />
<Input
placeholder='Email'
onChangeText= { (text)=> { this.setEmailText(text) } }
value={this.state.email}/>
</Item>
<Item>
<Icon type="FontAwesome" active name='key' style={{ color :'#c5aa6a'}} />
<Input
secureTextEntry
placeholder='Password'
onChangeText= { (text)=> { this.setPasswordText(text) } }
value={this.state.password}
/>
</Item>
<Item>
<Icon type="FontAwesome" active name='key' style={{ color :'#c5aa6a'}} />
<Input
secureTextEntry
placeholder='Confirm Password'
onChangeText= { (text)=> { this.setRePasswordText(text) } }
value={this.state.re_password}
/>
</Item>
<Button bordered success style={{ justifyContent: 'center', marginTop: 20}} onPress = { ()=> this.verifyCredentials() } >
<Text style={{color:'#c5aa6a'}}>Sign Up</Text>
</Button>
{
!this.state.avatarUri ?
<Icon type="FontAwesome" active name='user-circle' style={styles.photoPicker} onPress= { ()=> this.openCamera() } /> :
<Image style={styles.avatarPicked} source= {this.state.avatarUri} />
}
{
this.state.error ?
<Text style= {{ color: '#ff0000', alignSelf: 'center', marginTop: 30}}> {this.state.error} </Text>
:
null
}
</Content>
<Button transparent onPress={() => this.props.navigation.navigate('login')} style={styles.buttonStyle} >
<Text style={{color:'#c5aa6a'}}>I already have an account</Text>
</Button>
</Container>
编辑2
startSigningUp (email,password)
{
firebase.auth().createUserWithEmailAndPassword(email, password).then(
()=> {
const user = firebase.auth().currentUser;
const userId = user.uid;
this.setState({
user
})
this.uploadImage(this.state.avatarUri.uri,userId).then((imageURL)=>{
saveDatabase(userId,imageURL)
}).catch((error)=>{
this.setState({
error,
isLoading: false
})
})
})
}