我最近才开始尝试学习Meteor.js。我的目的是建立一个新闻源功能,可以在其中插入,编辑和删除内容。我被困在编辑部分。
imports / api / news / methods.js
Meteor.methods({
'news.insert'(content, title) {
check(content, String);
check(title, String);
// Make sure the user is logged in before insertig content
if (! Meteor.userId()) {throw new Meteor.Error('not-authorized');}
return News.insert({
content,
title,
createdAt: new Date(),
});
},
'news.remove'(newsId) {
check(newsId, String);
News.remove(newsId);
},
// Doesn't work?
'news.update'(content, title, newsId) {
check(content, String);
check(title, String);
check(newsId, String);
// user has to be logged in before updating content
if (! Meteor.userId()) {throw new Meteor.Error('not-authorized');}
News.update({_id: newsId}), {
$set: {
title:title,
content:content
}
}
}
});
我插入了一条通往内容更新表格的路线:
<a href="/apc_news/{{_id}}">Edit</a>
更新形式: imports / ui / components / news_edit / news_edit.html
<template name="NewsEdit">
<div class="col">
<small>Update: <b>{{news.content}}</b></small>
<form class='news-link-update'>
<input type="text" name="content" value="{{news.content}}" required> <small>ID: {{news._id}}</small> <br>
<br>
<textarea class="form-control" rows="5" id="comment" name="title" required>{{news.title}}</textarea>
<br>
<input type="submit" name="update" value="Update News" class="submit">
</form>
</div>
</template>
和调用该方法的js文件: imports / ui / components / news_edit / news_edit.js
import { News } from '/imports/api/news/news.js';
import { Meteor } from 'meteor/meteor';
import './news_edit.html';
Template.NewsEdit.helpers({
news: ()=> {
var id = FlowRouter.getParam('id');
return News.findOne({_id: id});
}
});
Template.NewsEdit.events({
'submit .news-link-update'(event) {
event.preventDefault();
const target = event.target;
let newsId = target.dataset.newsId;
const title = target.elements.title;
const content = target.elements.content;
Meteor.call('news.update', title.value, content.value, newsId, (error, result) => {
if (error) {
console.log(newsId);
alert(error.reason);
}
});
},
});
我不再遇到错误400,而是500错误。内部服务器错误。 该ID已被引用。
I20190222-04:22:58.230(1)? Exception while invoking method 'news.update' Error: Invalid modifier. Modifier must be an object.
I20190222-04:22:58.233(1)? at MongoConnection._update (packages/mongo/mongo_driver.js:495:11)
I20190222-04:22:58.233(1)? at MongoConnection.<anonymous> (packages\meteor.js:376:21)
I20190222-04:22:58.234(1)? at MongoConnection.(anonymous function) [as update] (packages/mongo/mongo_driver.js:754:49)
I20190222-04:22:58.234(1)? at _0xb3b0x8.(anonymous function).update (packages/mongo/collection.js:585:31)
I20190222-04:22:58.235(1)? at _0xb3b0x8.(anonymous function).Mongo.Collection.(anonymous function) [as update] (packages/aldeed:collection2/collection2.js:213:19)
I20190222-04:22:58.236(1)? at MethodInvocation.news.update (imports/api/news/methods.js:40:10)
I20190222-04:22:58.236(1)? at maybeAuditArgumentChecks (packages/ddp-server/livedata_server.js:1767:12)
I20190222-04:22:58.237(1)? at DDP._CurrentMethodInvocation.withValue (packages/ddp-server/livedata_server.js:719:19)
I20190222-04:22:58.237(1)? at Meteor.EnvironmentVariable.EVp.withValue (packages\meteor.js:1304:12)
I20190222-04:22:58.238(1)? at DDPServer._CurrentWriteFence.withValue (packages/ddp-server/livedata_server.js:717:46)
I20190222-04:22:58.239(1)? at Meteor.EnvironmentVariable.EVp.withValue (packages\meteor.js:1304:12)
I20190222-04:22:58.239(1)? at Promise (packages/ddp-server/livedata_server.js:715:46)
I20190222-04:22:58.242(1)? at new Promise (<anonymous>)
I20190222-04:22:58.242(1)? at Session.method (packages/ddp-server/livedata_server.js:689:23)
I20190222-04:22:58.243(1)? at packages/ddp-server/livedata_server.js:559:43
I20190222-04:23:28.905(1)? Exception while invoking method 'news.update' Error: Invalid modifier. Modifier must be an object.
I20190222-04:23:28.906(1)? at MongoConnection._update (packages/mongo/mongo_driver.js:495:11)
I20190222-04:23:28.910(1)? at MongoConnection.<anonymous> (packages\meteor.js:376:21)
I20190222-04:23:28.911(1)? at MongoConnection.(anonymous function) [as update] (packages/mongo/mongo_driver.js:754:49)
I20190222-04:23:28.911(1)? at _0xb3b0x8.(anonymous function).update (packages/mongo/collection.js:585:31)
I20190222-04:23:28.912(1)? at _0xb3b0x8.(anonymous function).Mongo.Collection.(anonymous function) [as update] (packages/aldeed:collection2/collection2.js:213:19)
I20190222-04:23:28.912(1)? at MethodInvocation.news.update (imports/api/news/methods.js:40:10)
I20190222-04:23:28.913(1)? at maybeAuditArgumentChecks (packages/ddp-server/livedata_server.js:1767:12)
I20190222-04:23:28.914(1)? at DDP._CurrentMethodInvocation.withValue (packages/ddp-server/livedata_server.js:719:19)
I20190222-04:23:28.915(1)? at Meteor.EnvironmentVariable.EVp.withValue (packages\meteor.js:1304:12)
I20190222-04:23:28.916(1)? at DDPServer._CurrentWriteFence.withValue (packages/ddp-server/livedata_server.js:717:46)
I20190222-04:23:28.918(1)? at Meteor.EnvironmentVariable.EVp.withValue (packages\meteor.js:1304:12)
I20190222-04:23:28.918(1)? at Promise (packages/ddp-server/livedata_server.js:715:46)
I20190222-04:23:28.919(1)? at new Promise (<anonymous>)
I20190222-04:23:28.920(1)? at Session.method (packages/ddp-server/livedata_server.js:689:23)
I20190222-04:23:28.920(1)? at packages/ddp-server/livedata_server.js:559:43
I20190222-04:25:34.580(1)? Exception while invoking method 'news.update' Error: Invalid modifier. Modifier must be an object.
I20190222-04:25:34.583(1)? at MongoConnection._update (packages/mongo/mongo_driver.js:495:11)
I20190222-04:25:34.584(1)? at MongoConnection.<anonymous> (packages\meteor.js:376:21)
I20190222-04:25:34.585(1)? at MongoConnection.(anonymous function) [as update] (packages/mongo/mongo_driver.js:754:49)
I20190222-04:25:34.585(1)? at _0xb3b0x8.(anonymous function).update (packages/mongo/collection.js:585:31)
I20190222-04:25:34.586(1)? at _0xb3b0x8.(anonymous function).Mongo.Collection.(anonymous function) [as update] (packages/aldeed:collection2/collection2.js:213:19)
I20190222-04:25:34.587(1)? at MethodInvocation.news.update (imports/api/news/methods.js:40:10)
I20190222-04:25:34.587(1)? at maybeAuditArgumentChecks (packages/ddp-server/livedata_server.js:1767:12)
I20190222-04:25:34.588(1)? at DDP._CurrentMethodInvocation.withValue (packages/ddp-server/livedata_server.js:719:19)
I20190222-04:25:34.593(1)? at Meteor.EnvironmentVariable.EVp.withValue (packages\meteor.js:1304:12)
I20190222-04:25:34.594(1)? at DDPServer._CurrentWriteFence.withValue (packages/ddp-server/livedata_server.js:717:46)
I20190222-04:25:34.594(1)? at Meteor.EnvironmentVariable.EVp.withValue (packages\meteor.js:1304:12)
I20190222-04:25:34.594(1)? at Promise (packages/ddp-server/livedata_server.js:715:46)
I20190222-04:25:34.595(1)? at new Promise (<anonymous>)
I20190222-04:25:34.596(1)? at Session.method (packages/ddp-server/livedata_server.js:689:23)
I20190222-04:25:34.597(1)? at packages/ddp-server/livedata_server.js:559:43
I20190222-04:27:21.274(1)? Exception while invoking method 'news.update' Error: Invalid modifier. Modifier must be an object.
I20190222-04:27:21.277(1)? at MongoConnection._update (packages/mongo/mongo_driver.js:495:11)
I20190222-04:27:21.278(1)? at MongoConnection.<anonymous> (packages\meteor.js:376:21)
I20190222-04:27:21.279(1)? at MongoConnection.(anonymous function) [as update] (packages/mongo/mongo_driver.js:754:49)
I20190222-04:27:21.280(1)? at _0xb3b0x8.(anonymous function).update (packages/mongo/collection.js:585:31)
I20190222-04:27:21.280(1)? at _0xb3b0x8.(anonymous function).Mongo.Collection.(anonymous function) [as update] (packages/aldeed:collection2/collection2.js:213:19)
I20190222-04:27:21.281(1)? at MethodInvocation.news.update (imports/api/news/methods.js:40:10)
I20190222-04:27:21.282(1)? at maybeAuditArgumentChecks (packages/ddp-server/livedata_server.js:1767:12)
I20190222-04:27:21.283(1)? at DDP._CurrentMethodInvocation.withValue (packages/ddp-server/livedata_server.js:719:19)
I20190222-04:27:21.283(1)? at Meteor.EnvironmentVariable.EVp.withValue (packages\meteor.js:1304:12)
I20190222-04:27:21.286(1)? at DDPServer._CurrentWriteFence.withValue (packages/ddp-server/livedata_server.js:717:46)
I20190222-04:27:21.286(1)? at Meteor.EnvironmentVariable.EVp.withValue (packages\meteor.js:1304:12)
I20190222-04:27:21.287(1)? at Promise (packages/ddp-server/livedata_server.js:715:46)
I20190222-04:27:21.288(1)? at new Promise (<anonymous>)
I20190222-04:27:21.289(1)? at Session.method (packages/ddp-server/livedata_server.js:689:23)
I20190222-04:27:21.289(1)? at packages/ddp-server/livedata_server.js:559:43
^这是新的错误日志。无效的修饰符部分是什么意思?
答案 0 :(得分:1)
由于服务器方法中的Error: Invalid modifier. Modifier must be an object.
语句存在语法错误,因此出现update
错误。
imports / api / news / methods.js
Meteor.methods({
...
// Doesn't work?
news.update(content, title, newsId) {
check(content, String);
check(title, String);
check(newsId, String);
// user has to be logged in before updating content
if (! Meteor.userId()) {throw new Meteor.Error('not-authorized');}
News.update({_id: newsId}, { //The parentheses was closed before the $set in your code
$set: {
title:title,
content:content
},
{
multi:false // optional since by default it is false
}
});
}
});