首页 > 解决方案 > putFile 不是函数

问题描述

我是 React Native 的新手,正在尝试为慈善机构创建一个应用程序。

尝试将使用相机拍摄的图像存储在 Firebase 存储中时遇到问题。

代码片段在下面,查看控制台日志,一切看起来都如我所料,也在下面。

我得到的错误是“imageRef.Putfile 不是函数”。

任何帮助将不胜感激。

控制台日志输出

ref, image, imageName breadalert-86646.appspot.com file:///data/user/0/host.exp.exponent/cache/ExperienceData/%2540anonymous%252Frn-complete-guide-10d15822-a070-46a4-a2a0-98550f263c74/ImagePicker/8211188f-6bf4-4aaf-a3ba-e187b1e3707f.jpg CKhLXQlDUtUvKotCYQmJLmOeI1s247
getting storage ref of breadalert-86646.appspot.com
firebaseStorageRef is gs://breadalert-86646.appspot.com/
Image is gs://breadalert-86646.appspot.com/CKhLXQlDUtUvKotCYQmJLmOeI1s247
Image Path is file:///data/user/0/host.exp.exponent/cache/ExperienceData/%2540anonymous%252Frn-complete-guide-10d15822-a070-46a4-a2a0-98550f263c74/ImagePicker/8211188f-6bf4-4aaf-a3ba-e187b1e3707f.jpg

代码片段

 saveImage = (ref, image, imageName, onSuccess, onError) => {


  console.log ('ref, image, imageName ' + ref + ' ' + image + ' ' + imageName);
  console.log ('getting storage ref of ' + ref);

  firebaseStorageRef = firebase.storage().ref();

  console.log ('firebaseStorageRef is ' + firebaseStorageRef);

  imageRef = firebaseStorageRef.child(imageName);
  console.log ('Image is ' + imageRef);
  console.log ('Image Path is ' + image);



  imageRef.putFile (image.path, {contentType: 'image/jpeg'}).then(function(){
      return imageRef.getDownloadURL();
  }).then(function(url){
    console.log ("Image url", {url:url});
      onSuccess(url);
  }).catch(function(error){
    console.log ("Error while saving the image.. ", error);
      onError(error);
  });
}

标签: javascriptfirebasereact-nativefirebase-storage

解决方案


imageRef是一个引用类型的对象。看看链接的 API 文档 - 它没有调用方法putFile()。这就是错误消息告诉您的内容。

如果要上传文件,则应使用名为put()的方法,如文档中所述。


推荐阅读