【问题标题】:reactjs firebase 'push' of undefined未定义的reactjs firebase“推送”
【发布时间】:2016-11-03 14:35:56
【问题描述】:

我在我的 reactjs 应用程序中定义了 firebase 如下(对于 firebase v2 的要求是正确的)

var App = React.createClass({
  mixins: [ReactFire],
  componentWillMount: function () {this.bindAsObject(new Firebase(rootURL + 'items/'), 'items')},
  render: function () {return <Header itemStore={this.firebaseRefs.items}/>}});

我将标题类中的道具称为休闲:

module.exports = React.createClass({
    getInitialState: function () {
        return {text: ''}
    },
    render: function () {return <button type="button" onClick={this.handleClick}>},
    handleClick: function () {this.props.itemStore.push({
            text:this.state.text,
            done:false
        });
        this.setState({text:''});
    }
});

但我面临Uncaught TypeError: Cannot read property 'push' of undefined的错误

【问题讨论】:

  • 看来您在items 中有错字。只需检查一下:componentWillMount: function () {this.bindAsObject(new Firebase(rootURL + 'items/'), 'itmes')}, //itmes 应该是我认为的项目
  • @cubbuk 谢谢,实际上在复制粘贴时输入错误,而不是在项目中:)

标签: reactjs firebase firebase-realtime-database


【解决方案1】:

您必须向您的班级添加以下内容:

    propTypes: {
        itemStore: ReactPropTypes.object
    }

你的课程最终会是这样的:

var ReactPropTypes = React.PropTypes;

module.exports = React.createClass({
    propTypes: {
        itemStore: ReactPropTypes.object
    },
    getInitialState: function () {
        return {text: ''}
    },

    render: function () {return <button type="button" onClick={this.handleClick}>},
    handleClick: function () {this.props.itemStore.push({
            text:this.state.text,
            done:false
        });
        this.setState({text:''});
    }
});

【讨论】:

  • 我已经从我的主类中创建了一个元素并使用 reactDOM 渲染它,并且不需要调度 props 服务。顺便说一句,我尝试了您的解决方案,但没有奏效,无论如何,谢谢。
  • 抱歉,它必须是以下内容:propTypes: { itemStore: ReactPropTypes.object }
  • propTypes: { itemStore: ReactPropTypes.object }
  • 我应用了更改,但同样的问题,Firebase 的实例似乎根本没有“推送”。
  • 我猜问题出在架构上。您应该在 store 中声明一个函数,该函数将推送到您保存项目的 store 变量。然后你应该调用该函数并将对象作为参数放入该调用中。
【解决方案2】:

这里的问题是 this.props.itemStore 是未定义的。未定义类型没有可用的“推送”方法。

this.props.itemStore 在单击处理程序中进行控制台记录然后单击时会是什么样子?如果未定义,this.props 是什么样的?

【讨论】:

    猜你喜欢
    • 2016-07-27
    • 2020-04-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多