//My collection
import { Mongo } from 'meteor/mongo';
import PostSchema from './schema';
const Posts = new Mongo.Collection('Posts');
Posts.attachSchema(PostSchema);
export default Posts;
//My schema
import SimpleSchema from "simpl-schema";
export default new SimpleSchema({
title: {
type: String
},
description: {
type: String
},
createdAt: {
type: Date,
autoValue: function() {
if (this.isInsert) {
return new Date();
} else if (this.isUpsert) {
return {$setOnInsert: new Date()};
} else {
this.unset(); // Prevent user from supplying their own value
}
}
},
userId: {
type: String,
autoValue: function(){ return this.userId }
}
});
//My methods file
import {Meteor} from "meteor/meteor";
import Posts from "/imports/api/posts/collection";
import Security from '/imports/api/security.js';
Meteor.methods({
'post_create' (data) {
if (!data) {
throw new Meteor.Error('error', 'Make sure you have filled all the fields! ;)', 'Some details');
} else if (Security.checkLoggedIn(this.userId)) {
console.log("Are you sure you logged in?");
} else {
return Posts.insert(data);
}
},
'post_get': function (id) {
if (Security.checkLoggedIn(this.userId)) {
console.log("Are you sure you logged in?");
} else {
return Posts.find({id}).fetch();
}
},
'post_remove': function (id) {
if (Security.checkLoggedIn(this.userId)) {
console.log("Are you sure you logged in?");
} else {
return Posts.remove({_id: id});
}
},
//My problem is here! When I call this method in the postEdit.jsx file i get title prop undefined
'post_edit': function (data) {
if (Security.checkLoggedIn(this.userId)) {
console.log("Are you sure you logged in?");
} else {
Posts.update(data,{
$set: {
title: this.data.title,
description: data.description
}
});
}enter code here
}
});
//My PostEdit.jsx
import React from "react";
import {Meteor} from "meteor/meteor";
import {createContainer} from "meteor/react-meteor-data";
import {AutoForm, AutoField, TextField} from "uniforms-unstyled";
import PostCreateSchema from "./PostCreateSchema";
export default class PostsEdit extends React.Component {
constructor() {
super();
this.state = {loading: true, Posts: []}
}
componentDidMount() {
Meteor.call('post_get', (err, res) => {
this.setState({
loading: false,
Posts: res
})
})
}
handleEdit(data) {
Meteor.call('post_edit',data, (err, res)=> {
if (err) {
console.log('Register error', err);
} else {
console.log("Your post have been changed", res);
}
})
}
render() {
if (this.state.loading) {
return <div>Waiting for the method</div>
}
return (
<div>
{
<ul>
{
this.state.Posts.map(posts => {
return (
<AutoForm schema={PostCreateSchema}
onSubmit={this.handleEdit.bind(this)}
placeholder
key={posts._id}
>
<h4>Title: {posts.title}</h4>
<h4>Description: {posts.description}</h4>
<AutoField name="title"/>
<TextField name="description"/>
<button type="submit" className="btn btn-default">Save</button>
</AutoForm>
);
})
}
</ul>
}
</div>
)
}
}
//我正在努力了解如何解决这个问题。我知道问题出在参数上,并且它们是未定义的....但我不知道如何为密钥赋予正确的值。任何提示/链接/建议都是高度赞成的。