【问题标题】:Unhandled Rejection (Error): Reference.set failed: First argument contains undefined in property 'signup.test.Robots.props.history.location.state'未处理的拒绝(错误):Reference.set 失败:第一个参数在属性“signup.test.Robots.props.history.location.state”中包含未定义
【发布时间】:2020-09-27 10:37:29
【问题描述】:

我的 react 文件中的切换按钮不会向 firebase 发送任何数据。代码返回此错误:未处理的拒绝(错误):Reference.set failed: First argument contains undefined in property 'signup.test.Robots.props.history.location.state'。

我也定义了我所有的道具。如果您发现错误,请帮助我。

按钮:

 <div data-toggle="buttons" class="btn-group bizmoduleselect mt-2 lg:w-1/3">
                                                

                                                    <label class="btn btn-default mx-1  rounded " >
                                                        <div class="bizcontent  py-2" id="bizcontent">
                                                            <input type="checkbox" id="checkbox" name="Space" autocomplete="off" value="Space" hidden 
                                                            checked={this.state.isSpace}
                                                            onChange={this.onChangeSpace} />
                                                            <span class="glyphicon glyphicon-ok glyphicon-lg"></span>
                                                            <p>Space</p>
                                                        </div>
                                                    </label>
                                                    <label class="btn btn-default mx-1 rounded">
                                                        <div class="bizcontent py-2 "  >
                                                            <input type="checkbox" id="checkbox" name="Robots" autocomplete="off" value="Robots" hidden 
                                                             checked={this.state.Robots}
                                                             onChange={this.onChangeRobots}/>
                                                            <span class="glyphicon glyphicon-ok glyphicon-lg"></span>
                                                            <p>Robots</p>
                                                        </div>
                                                    </label>
                                                    
                                                    <label class="btn btn-default mx-1 rounded">
                                                        <div class="bizcontent  py-2">
                                                            <input type="checkbox" name="Magic" autocomplete="off" value="Magic" hidden 
                                                            checked={this.state.isMagic}
                                                            onChange={this.onChangeMagic} />
                                                            <span class="glyphicon glyphicon-ok glyphicon-lg"></span>
                                                            <p>Magic tricks</p>
                                                        </div>
                                                    </label></div>

状态和道具:

constructor(props) {
        super(props);
        this.state = {
            name: '',
            age: '',
            email: '',
            phone_number: '',
            redirect: false,
            showAlert: false,
            alertMessage: '',
            isSpace: '',
            Robots: '',
            Magic: '',
            
        }
    
        this.signup = this.signup.bind(this);
        this.onNameChange = this.onNameChange.bind(this);
        this.onAgeChange = this.onAgeChange.bind(this);
        this.onEmailChange = this.onEmailChange.bind(this);
        this.onNumberChange = this.onNumberChange.bind(this);
        this.onChangeSpace = this.onChangeSpace.bind(this);
        this.onChangeMagic = this.onChangeMagic.bind(this);
        this.onChangeRobots = this.onChangeRobots.bind(this);




    }

    
    onChangeSpace = () => {
        console.log("onChanges");
        this.setState(initialState => ({
          isSpace: !initialState.isSpace,
        }));
      }
    
      onChangeMagic = () => {
        console.log("onChanges");
        this.setState((currentState) => ({
            Magic: currentState.Magic, 
        }));
      }
      onChangeRobots = () => {
        console.log("onChanges");
        this.setState((currentState) => ({
            Robots: currentState.Robots, 
        }));
      }

向 Firebase 发送数据:

    import firebase from '../firebase.js';

export function register(email, name, age, phone_number, isSpace, Robots, Magic, isArt, isMusic, _this) {

    firebase.database().ref('/signup/' + name).once('value').then(function (snapshot) {
        if (snapshot.val() === null || snapshot.val() === undefined) {
            firebase.database().ref('signup/' + name).set({
                name: name,
                age: age,
                email: email,
                phone_number: phone_number,
                isSpace: isSpace,
                Robots: Robots,
                Magic: Magic,
                
            });
            
        }

    });
}

【问题讨论】:

  • 您能否在将 Robots 属性内容发送到 firebase 之前对其进行记录?
  • 是的,我尝试使用 console.log,但控制台上没有打印(似乎状态没有改变?)

标签: javascript html reactjs firebase firebase-realtime-database


【解决方案1】:

错误消息告诉您向set() 传递了一个无效值。仔细阅读:

Reference.set 失败:第一个参数在属性“signup.test.Robots.props.history.location.state”中包含未定义

表示路径signup.test.Robots.props.history.location.state 的值未定义。因此,听起来您作为Robots 传递的任何内容都包含一个未定义的嵌套属性props.history.location.state。您必须进行一些调试才能找出原因。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-26
    • 2015-11-06
    相关资源
    最近更新 更多