Javascript 更新Meteor.js中的帖子
我最近刚开始尝试学习Meteor.js。我的目的是建立一个新闻提要功能,在这里我可以插入、编辑和删除内容。我被困在编辑部分 导入/api/news/methods.js 我插入了一条通向内容更新表单的路径:Javascript 更新Meteor.js中的帖子,javascript,meteor,updates,Javascript,Meteor,Updates,我最近刚开始尝试学习Meteor.js。我的目的是建立一个新闻提要功能,在这里我可以插入、编辑和删除内容。我被困在编辑部分 导入/api/news/methods.js 我插入了一条通向内容更新表单的路径: <a href="/apc_news/{{_id}}">Edit</a> 我不再得到错误400,而是错误500。内部服务器错误。 该id现在被引用 I20190222-04:22:58.230(1)? Exception while invoking method
<a href="/apc_news/{{_id}}">Edit</a>
我不再得到错误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
^这是新的错误日志。无效修饰符部分是什么意思?您得到错误:无效修饰符。修改器必须是对象。错误,因为服务器方法的update语句中存在语法错误
导入/api/news/methods.js
您的代码中没有引用newsId的地方。在“submit.news update link”事件中,尝试从窗体和控制台获取newsId。在将值传递给Meteor.call方法之前记录这些值。不再出现错误400,但会出现错误500。newsId现在被引用。
<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>
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);
}
});
},
});
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
Meteor.methods({
...
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');}
// *** The parentheses was closed before '$set' was passed ***
News.update({_id: newsId}, {
$set: {
title:title,
content:content
},
{
multi:false // optional since by default it is false
}
});
}
});