我尝试将base64图像上传到Firebase存储并收到此错误:
assign的一个来源在原型链上有一个可枚举的键。您是否尝试分配原型属性?我们不允许这样做,因为这是我们不支持的边缘情况。
在EXPO@19.0.0上运行(在v18上同样的问题)和React@16.0.0-alpha.12(与Expo v19一起提供)。
使用的功能如下 来源:https://github.com/aaronksaunders/expo-rn-firebase-image-upload/blob/master/README.md
const addSignService = (signData, imageAsBase64, filename) => {
_uploadAsByteArray(convertToByteArray(imageAsBase64), filename, (progress) => {
console.log('_uploadAsByteArray callback '+progress);
//this.setState({ progress })
});
}
const _uploadAsByteArray = async (pickerResultAsByteArray, filename, progressCallback) => {
console.log('_uploadAsByteArray');
try {
var metadata = {
contentType: 'image/jpeg',
};
var ref = storageRef.child('images/'+ firebaseAuth().currentUser.uid + '/' + filename);
console.log('before upload');
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');
console.log('log test');
}, function (error) {
console.log("in _uploadAsByteArray ", error);
}, function () {
console.log('function');
var downloadURL = uploadTask.snapshot.downloadURL;
console.log("_uploadAsByteArray ", uploadTask.snapshot.downloadURL)
});
} catch (ee) {
console.log("when trying to load _uploadAsByteArray ", ee)
}
}
const convertToByteArray = (input) => {
console.log('convertToByteArray');
var binary_string = atob(input);
var len = binary_string.length;
var bytes = new Uint8Array(len);
for (var i = 0; i < len; i++) {
bytes[i] = binary_string.charCodeAt(i);
}
return bytes
}
const atob = (input) => {
console.log('atob');
const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
let str = input.replace(/=+$/, '');
let output = '';
if (str.length % 4 == 1) {
throw new Error("'atob' failed: The string to be decoded is not correctly encoded.");
}
for (let bc = 0, bs = 0, buffer, i = 0;
buffer = str.charAt(i++);
~buffer && (bs = bc % 4 ? bs * 64 + buffer : buffer,
bc++ % 4) ? output += String.fromCharCode(255 & bs >> (-2 * bc & 6)) : 0
) {
buffer = chars.indexOf(buffer);
}
return output;
}
答案 0 :(得分:2)
在调用Firebase.put()
时激活React Native / iOS远程调试器时,会出现此错误。取消激活调试器解决了错误,函数正常工作。
感谢Github上的aaronksaunders寻求帮助。 https://github.com/aaronksaunders/expo-rn-firebase-image-upload/issues/3