首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >无法将数据从indexedDB传递给角度主题,以便在可观察的使用ngx db中使用。

无法将数据从indexedDB传递给角度主题,以便在可观察的使用ngx db中使用。
EN

Stack Overflow用户
提问于 2019-08-22 16:01:17
回答 1查看 529关注 0票数 1

在使用getByKey从indexedDB调用ngx db之后,我希望将数据传递给主题。我可以看到数据正在被调用,但是当我使用next时,我被告知数据是未定义的。

代码语言:javascript
运行
复制
public userInfo = new Subject<any>();

 getData(){ 
    var db = new NgxIndexedDB('jwt', 1);    
    db.openDatabase(1).then(function() {
      db.getByKey('token', 1).then(
        (res) => {
            // Do something after the value was added
            console.log(res); //data populates in the console
            this.userInfo.next(res) // this step I receive undefined error
        },
        error => {
            console.log(error);
        }
    );
  }

watchUser(): Observable<any> {
  return this.userInfo.asObservable();        
};

getUserInfo(){
  this.watchUser().subscribe(res => console.log(res))
}

错误:未知(承诺):TypeError:无法读取未定义TypeError的属性“userInfo”:无法读取未定义的属性“userInfo”

在第118行中,您可以看到console.log显示数据是从indexedDB中提取出来的,在第119行中,您可以看到没有数据被传递,并且正在以未定义的方式通过。我希望传递来自indexedDB的数据,以便在应用程序的其余部分中使用。

EN

Stack Overflow用户

发布于 2019-08-22 16:07:07

请您检查一下BehaviorSubject好吗?也许会有帮助的。别忘了导入它。

代码语言:javascript
运行
复制
public userInfo = new BehaviorSubject<any>('');
票数 0
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57613220

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档