大家好,所以我按照本教程https://www.meteor.com/tutorials/react/security-with-methods
进入第9步所以我用他们的检查编写了所有方法,但是我在调用方法'tasks.remove'时遇到异常错误错误:匹配错误:预期字符串,得到对象
这是我的书面代码 这是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 } })
}
})
这就是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
}
我编写的代码与教程代码相同的问题是什么,为什么我会收到这些错误?我对更新方法有同样的错误。
编辑:评论检查并执行教程的后续步骤,然后启用检查使它们工作......但我不确定哪个部分使它们起作用
答案 0 :(得分:0)
check
函数需要一个String,因为您将String
作为参数进行检查。但是你的React Task
组件期待Object。
Task.propTypes = {
task: PropTypes.object.isRequired
}
试试
'tasks.remove' (taskId) {
check(taskId, Object)
Tasks.remove(taskId)
},
而不是
'tasks.remove' (taskId) {
check(taskId, String)
Tasks.remove(taskId)
},
答案 1 :(得分:0)
它仍然会出现“对象”错误。尝试使用javascript String()函数 -
进行协调输入 '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 } });
},