我有这个功能将图像插入firebase。我要做的是选择图像,将图像插入firebase存储,获取其downloadURL,然后插入帐户的新记录以及downloadURL。
uploadAsByteArray = async (pickerResultAsByteArray, progressCallback) => {
try {
var metadata = {
contentType: 'image/jpeg',
};
let name = new Date().getTime() + "-media.jpg"
var storageRef = firebase.storage().ref();
var ref = storageRef.child('assets/' + name)
let uploadTask = ref.put(pickerResultAsByteArray, metadata)
uploadTask.on('state_changed', function (snapshot) {
progressCallback && progressCallback(snapshot.bytesTransferred / snapshot.totalBytes)
var progress = (snapshot.bytesTransferred / snapshot.totalBytes) * 100;
console.log('Upload is ' + progress + '% done');
}, function (error) {
console.log("in _uploadAsByteArray ", error)
}, function () {
var downloadURL = uploadTask.snapshot.downloadURL;
//console.log("_uploadAsByteArray ", uploadTask.snapshot.downloadURL)
console.log('URLLLLL ' + downloadURL);
// save a reference to the image for listing purposes
var ref = firebase.database().ref('assets');
ref.push({
'URL': downloadURL,
//'thumb': _imageData['thumb'],
'name': name,
//'coords': _imageData['coords'],
'owner': firebase.auth().currentUser && firebase.auth().currentUser.uid,
'when': new Date().getTime()
})
});
return downloadURL;
} catch (ee) {
console.log("when trying to load _uploadAsByteArray ", ee)
}
}
我想从这个函数返回变量downloadURL并将其作为参数传递给我的addNewAccount(),我得到了未定义。
addNewAccount = (downloadURL) => {
console.log(downloadURL);
}
答案 0 :(得分:0)
您需要使用回叫,就像使用progressCallback
一样,然后传递downloadURL
:
然后,您将downloadURL
正在进行中:
api.uploadAsByteArray(byteArray, (downloadURL) => {
//console.log('downloadURL' + downloadURL)
this.setState({ downloadURL: downloadURL})
})
在setState
之后,您将在组件状态下拥有downloadURL,并且可以在addNewAccount
中传递它:
<TouchableOpacity onPress={()=> this.addNewAccount(downloadURL)}>
你的函数uploadAsByteArray:
uploadAsByteArray = async(pickerResultAsByteArray, progressCallback) => {
try {
var metadata = {
contentType: 'image/jpeg',
};
let name = new Date().getTime() + "-media.jpg"
var storageRef = firebase.storage().ref();
var ref = storageRef.child('assets/' + name)
let uploadTask = ref.put(pickerResultAsByteArray, metadata)
uploadTask.on('state_changed', function(snapshot) {
progressCallback && progressCallback(snapshot.bytesTransferred / snapshot.totalBytes)
var progress = (snapshot.bytesTransferred / snapshot.totalBytes) * 100;
console.log('Upload is ' + progress + '% done');
}, function(error) {
console.log("in _uploadAsByteArray ", error)
}, function() {
var downloadURL = uploadTask.snapshot.downloadURL;
//console.log("_uploadAsByteArray ", uploadTask.snapshot.downloadURL)
console.log('URLLLLL ' + downloadURL);
// save a reference to the image for listing purposes
var ref = firebase.database().ref('assets');
ref.push({
'URL': downloadURL,
//'thumb': _imageData['thumb'],
'name': name,
//'coords': _imageData['coords'],
'owner': firebase.auth().currentUser && firebase.auth().currentUser.uid,
'when': new Date().getTime()
})
progressCallback(downloadURL);
});
} catch (ee) {
console.log("when trying to load _uploadAsByteArray ", ee)
}
}