Reputation: 2056
Hi guys so I got to step 9 by following this tutorial https://www.meteor.com/tutorials/react/security-with-methods
So i've written all the methods with their checks however I get errors like Exception while invoking method 'tasks.remove' Error: Match error: Expected string, got object
Here are my written codes This is the tasks.js
import { Meteor } from 'meteor/meteor'
import { Mongo } from 'meteor/mongo'
import { check } from 'meteor/check'
export const Tasks = new Mongo.Collection('tasks')
Meteor.methods({
'tasks.insert' (text) {
check(text, String)
// Make sure the user is logged in before insterting a task
if (!this.userId) {
throw new Meteor.Error('not-authorized')
}
Tasks.insert({
text,
createdAt: new Date(),
owner: this.userId,
username: Meteor.users.findOne(this.userId).username
})
}, // tasks.insert
'tasks.remove' (taskId) {
check(taskId, String)
Tasks.remove(taskId)
},
'tasks.setChecked' (taskId, setChecked) {
check(taskId, String)
check(setChecked, Boolean)
Tasks.update(taskId, { $set: { checked: setChecked } })
}
})
And this is the Task.jsx
import React, { Component, PropTypes } from 'react'
import { Meteor } from 'meteor/meteor'
// import { Tasks } from '../api/tasks.js'
// Task component - represents a single todo item
export default class Task extends Component {
toggleChecked () {
// Set the checked value to the opposite of its current value
Meteor.call('tasks.setChecked',this.props.task._id, !this.props.task.checked)
}
deleteThisTask () {
Meteor.call('tasks.remove', this.props.task._id)
}
render () {
// Give tasks a different className when they are checked off,
// so that we can style them nicely
const taskClassName = this.props.task.checked ? 'checked' : ''
return (
<li className={taskClassName}>
<button className="delete" onClick={this.deleteThisTask.bind(this)}>
×
</button>
<input
type="checkbox"
readOnly
checked={this.props.task.checked}
onClick={this.toggleChecked.bind(this)}
/>
<span className="text">
<strong>{this.props.task.username}</strong>:{this.props.task.text}
</span>
</li>
)
}
}
Task.propTypes = {
// This component gets the task to dipslay through a React prop.
// We can use propTypes to indicate it is required
task: PropTypes.object.isRequired
}
What's the problem my written code seems to be identical with the tutorials code, why then do I get these errors? I got the same error for the update method.
EDIT: After commenting the checks and doing the later steps of the tutorial and then enabling the checks makes them work... but I am not sure which part made them to work
Upvotes: 0
Views: 536
Reputation: 1
It still gives error with 'Object'. Try concerting input with javascript String() function -
'tasks.remove'(taskId) {
check(String(taskId), String);
Tasks.remove(taskId);
},
'tasks.setChecked'(taskId, setChecked) {
check(String(taskId), String);
check(setChecked, Boolean);
Tasks.update(taskId, { $set: { checked: setChecked } });
},
Upvotes: 0
Reputation: 305
The check
function is expecting a String because you passed String
as a parameter for checking. But your React Task
component is expecting Object.
Task.propTypes = {
task: PropTypes.object.isRequired
}
Just try
'tasks.remove' (taskId) {
check(taskId, Object)
Tasks.remove(taskId)
},
instead of
'tasks.remove' (taskId) {
check(taskId, String)
Tasks.remove(taskId)
},
Upvotes: 0