【发布时间】: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