当前位置: 首页 > 知识库问答 >
问题:

未捕获的类型错误:无法读取未定义 [重复] 的属性“存储”

金兴朝
2023-03-14

我正在上传一张图片到firebase,然后试图将它的下载URL保存到< code>ionic storage,但是它给了我这个错误

未捕获类型错误:无法读取未定义的属性“存储”

以下是我的代码:

import { Injectable } from '@angular/core';
import { AngularFirestore, AngularFirestoreCollection } from '@angular/fire/firestore';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
import * as firebase from 'firebase';
import { Storage } from '@ionic/storage';
import uuid from 'uuid/v1'; //here change 'v1' with the version you desire to use

export interface Dress {
  id?: string;
  title: string;
  description: string;
  createdAt: number;
  category: string;
  price: number;
  city: string;
  type: string;
  size: string;
  action: string;
  image_1: string;
  image_2: string;
  image_3: string;
}
export interface Category {
  name: string;
}
export interface City {
  name: string;
}
export interface Type {
  name: string;
}
export interface Size {
  name: string;
}
export interface Action {
  name: string;
}

@Injectable({
  providedIn: 'root'
})
export class DressService {
  private dressCollection: AngularFirestoreCollection<Dress>;
  private dress: Observable<Dress[]>;
  private categoryCollection: AngularFirestoreCollection<Category>;
  private category: Observable<Category[]>;
  private cityCollection: AngularFirestoreCollection<City>;
  private city: Observable<City[]>;
  private typeCollection: AngularFirestoreCollection<Type>;
  private type: Observable<Type[]>;
  private sizeCollection: AngularFirestoreCollection<Size>;
  private size: Observable<Size[]>;
  private actionCollection: AngularFirestoreCollection<Action>;
  private action: Observable<Action[]>;
  id = uuid();

  constructor(db: AngularFirestore,
              public storage: Storage,
  ) {
    this.dressCollection = db.collection<Dress>('dress');
    this.dress = this.dressCollection.snapshotChanges().pipe(
      map(actions => {
        return actions.map(a => {
          const data = a.payload.doc.data();
          const id = a.payload.doc.id;
          return { id, ...data };
        });
      })
    );
    this.categoryCollection = db.collection<Category>('categories');
    this.category = this.categoryCollection.snapshotChanges().pipe(
      map(actions => {
        return actions.map(a => {
          const data = a.payload.doc.data();
          const id = a.payload.doc.id;
          return { id, ...data };
        });
      })
    );
    this.cityCollection = db.collection<City>('cities');
    this.city = this.cityCollection.snapshotChanges().pipe(
      map(actions => {
        return actions.map(a => {
          const data = a.payload.doc.data();
          const id = a.payload.doc.id;
          return { id, ...data };
        });
      })
    );
    this.typeCollection = db.collection<Type>('types');
    this.type = this.typeCollection.snapshotChanges().pipe(
      map(actions => {
        return actions.map(a => {
          const data = a.payload.doc.data();
          const id = a.payload.doc.id;
          return { id, ...data };
        });
      })
    );
    this.sizeCollection = db.collection<Size>('sizes');
    this.size = this.sizeCollection.snapshotChanges().pipe(
      map(actions => {
        return actions.map(a => {
          const data = a.payload.doc.data();
          const id = a.payload.doc.id;
          return { id, ...data };
        });
      })
    );
    this.actionCollection = db.collection<Action>('actions');
    this.action = this.actionCollection.snapshotChanges().pipe(
      map(actions => {
        return actions.map(a => {
          const data = a.payload.doc.data();
          const id = a.payload.doc.id;
          return { id, ...data };
        });
      })
    );
  }
  getDresses() {
    return this.dress;
  }
  getCategories() {
    return this.category;
  }
  getTypes() {
    return this.type;
  }
  getCities() {
    return this.city;
  }
  getSizes() {
    return this.size;
  }
  getActions() {
    return this.action;
  }
  getDress(id) {
    return this.dressCollection.doc<Dress>(id).valueChanges();
  }
  updateDress(dress: Dress, id: string) {
    return this.dressCollection.doc(id).update(dress);
  }
  addDress(dress: Dress) {
    return this.dressCollection.add(dress);
  }
  removeDress(id) {
    return this.dressCollection.doc(id).delete();
  }
  uploadImage(img, numb) {
    const ref = firebase.database().ref('Uploads');
    const storage = firebase.storage();
    const pathReference = storage.ref('images/' + this.id + numb + '.jpg');
    const message = img;
    pathReference.putString(message, 'base64', { contentType: 'image/jpg' }).then(function (snapshot) {
      console.log('Uploaded a base64url string!');
      pathReference.getDownloadURL().then(function (url) {
        console.log(url);
        console.log(typeof(url));
        if (numb === 1) {
          this.storage.set('image_1_url', url);
        }
        if (numb === 2) {
          this.storage.set('image_2_url', url);
        }
        if (numb === 3) {
          this.storage.set('image_3_url', url);
        }
      });
    });


  }
}

当我选中图像后调用上传图像()函数时,它已经上传并生成了URL,但无法保存,错误来自这一行

        if (numb === 1) {
          this.storage.set('image_1_url', url);
        }

我有我的Firebase的适当配置,一切都完美运行,只有在离子存储部分时才会失败


共有1个答案

景育
2023-03-14

请在匿名函数上使用粗箭头函数,因为前者不会创建自己的执行范围,而后者会创建,因此您的“this”会开始指向它:

uploadImage(img, numb) {
    const ref = firebase.database().ref('Uploads');
    const storage = firebase.storage();
    const pathReference = storage.ref('images/' + this.id + numb + '.jpg');
    const message = img;
    pathReference.putString(message, 'base64', { contentType: 'image/jpg' }).then(function (snapshot) {
      console.log('Uploaded a base64url string!');
      // see here replaced 'function()' with =>:
      pathReference.getDownloadURL().then((url)=>{
        console.log(url);
        console.log(typeof(url));
        if (numb === 1) {
          this.storage.set('image_1_url', url);
        }
        if (numb === 2) {
          this.storage.set('image_2_url', url);
        }
        if (numb === 3) {
          this.storage.set('image_3_url', url);
        }
      });
    });


  }
 类似资料:
  • 我正在选择area_enninFantry的所有子div,并循环调整某些子div的文本。cardArray是一个全局常量,myId在父函数中定义。 未捕获的TypeError:无法读取未定义的属性(读取“getAttribute”) 我在这篇文章中读到,这可能是因为字段/元素的内容可能是DOM元素,我应该用$()包装它们,所以我确实这样做了——将两个变量都更改为$(元素)[a]. attr(“标题

  • 我遇到了一个有趣的问题,这使我感到困惑。我正在执行一个正确返回数据的Sharepoint RestAPI调用,当我在数据上运行for循环时,它生成html,但仍然抛出我用作标题的错误。代码如下。如果我记录每个循环,它将返回值。HTML也可以很好地工作。问题是错误仍然会出现。

  • 我已经在WordPress中开发了一个网站,并安装了重力表单插件(http://www.gravityforms.com/). 我已将book online表单添加到页面,但当单击时,datepicker在控制台中返回以下错误: 未捕获的TypeError:无法读取未定义的属性“msie” http://ultimatepaintball.com.au/packages/book-online/

  • 我使用的是最新版本的react路由器(版本^3.0.0)。 我使用ES5编写了以下路由: : 在另一个名为

  • 我想有一个无限滚动的项目列表,我正在使用这个包https://github.com/CassetteRocks/react-infinite-scroller但是在留档中没有提到抓取功能的样子。 在我的代码中,它是这样的: 完整消息错误: 未捕获(promise中)类型错误:无法读取评估时未定义的属性“companys”(traineships.component.js:71) 编辑:(我不再有错

  • 我正在使用NVD3库根据Rails控制器中生成的数据制作简单的折线图。我用来在Rails中生成数据的代码是: 然后,在我看来,我有以下JS代码: 图表正确呈现,但当我尝试将鼠标悬停在数据点上以显示工具提示时,会出现错误“Uncaught TypeError:无法读取未定义的属性“x”