【问题标题】:Meteor findOne not returning value流星findOne没有返回值
【发布时间】:2023-04-03 18:03:01
【问题描述】:

我有一个订阅项目的组件。 这些项目被确认在那里,就好像我使用以下它返回带有项目的光标:

console.log(Items.find({key: itemKey}).fetch());

项目集合被导入等

我的构造函数:

constructor(){
            super();
            this.state = {
                    subscription: {
                            items: Meteor.subscribe("allItems")
                    }
            }
    }

我的组件将卸载方法:

    componentWillUnmount(){
            this.state.subscription.items.stop();
    }

这是问题所在的方法。错误是 Uncaught TypeError: Cannot read property 'srcEntire' of undefined:

    getItemSource(itemKey){
           console.log(itemKey); // shows the correct key
           var itemer = Items.findOne({key: itemKey});
           console.log(itemer.srcEntire);
            return itemer.srcEntire;
    }

渲染部分(如果我硬编码getItemSource方法的返回值,它会显示图像。通过调用getItemSource方法并传入传入的图层道具(道具工作)找到图像的来源:

    render(){   


     return (                               
              <div className="col-lg-3" >

             <img id="myLayer" className="on-top img-responsive center-block on-top " name="myLayer" src={this.getItemSource(this.props.layers.myLayer)} />

</div>

)}

【问题讨论】:

    标签: javascript mongodb meteor reactjs


    【解决方案1】:

    您的订阅尚未准备好,当您执行 findOne() 时,itemer 将是未定义的,直到它(可能是毫秒)。您可以等待订阅准备就绪,也可以通过以下方式为自己辩护:

    getItemSource(itemKey){
      var itemer = Items.findOne({key: itemKey});
      return itemer && itemer.srcEntire;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-26
      • 2016-10-08
      • 1970-01-01
      • 2017-01-10
      相关资源
      最近更新 更多