Ich bin neu mit Meteor reagieren und ich habe Probleme, ein Formular zu erhalten, mit vorhandenen Daten, die aus einer veröffentlichten Sammlung geladen wird. Ich veröffentliche erfolgreich die Sammlung, aber wenn ich versuche, auf profileCandidate
im Konstruktor this.state
zuzugreifen, wird das Formular nicht geladen. Kann mir jemand zeigen, was ich hier falsch mache?Populate Meteor React Formular mit vorhandenen Daten
Collection: profileCandidate
{
"_id": "JGw6dTHG3RDjDQNXc",
"userId": "fYHKGTRhZvPKCETHQ",
"createdAt": "2017-04-25T12:05:30.449Z",
"name": {
"first": "John",
"last": "Doe"
}
}
Komponente: ‚ProfileCandidateForm.jsx`
class ProfileCandidateForm extends Component {
constructor(props) {
super(props);
this.state = {
firstName: [],
lastName: []
};
this.handleChange = this.handleChange.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
}
handleChange(event) {
const target = event.target;
const value = target.value;
const name = target.name;
this.setState({
[name]: value
});
}
handleSubmit(event) {
event.preventDefault();
profileCandidate = this.state;
Meteor.call('profileCandidate.insert', profileCandidate);
}
render() {
return (
<form onSubmit={this.handleSubmit.bind(this)}>
<label>
Name:
</label>
<input
type="text"
name="firstName"
value={this.state.firstName}
onChange={this.handleChange}
placeholder="First name"
/>
<input
type="text"
name="lastName"
value={this.state.lastName}
onChange={this.handleChange}
placeholder="Last name"
/>
<input
type="submit"
value="Submit"
/>
</form>
)
}
}
ProfileCandidateForm.propTypes = {
profileCandidate: PropTypes.object.isRequired,
}
export default createContainer(() => {
Meteor.subscribe('profileCandidate');
return {
profileCandidate: ProfileCandidate.findOne({userId: Meteor.userId()}),
};
}, ProfileCandidateForm);
Können Sie console.log 'this.props'? –
Ich habe 'console.log (this.props) 'unter' super (reps) 'gesetzt und es hat' Object {profileCandidate: Array (0)} ' – bp123
zurückgegeben, wenn Sie im Konstruktor console.log requisiten, denke ich, dass es normal ist Leer, weil die Komponente noch keine Requisiten erhalten hat. Erwarten Sie außerdem, dass die Auflistung ProfileCandidate ein leeres Array zurückgibt? –