Mongodb 为什么我不能在服务器上使用Meteor.method?
我正在尝试向登录用户集合添加url。我的最终目标至少是能够添加一个字段,例如Mongodb 为什么我不能在服务器上使用Meteor.method?,mongodb,collections,meteor,meteorite,Mongodb,Collections,Meteor,Meteorite,我正在尝试向登录用户集合添加url。我的最终目标至少是能够添加一个字段,例如{profilePicture:'http://randompic.com/123.png“} 到目前为止,我尝试的是: <template name="profile"> <h1>My Profile</h1> <form class="form-inline"action=""> <label for="url"></label>
{profilePicture:'http://randompic.com/123.png“}
到目前为止,我尝试的是:
<template name="profile">
<h1>My Profile</h1>
<form class="form-inline"action="">
<label for="url"></label>
<input class="input input-large" type="url" name="url" placeholder="URL for you image..." id="url">
<button class="btn btn-success submit">Update profile picture</button>
</form>
</template>
我已尝试提醒选项。\u id和options.profilePicture,我有可用的数据。
现在,当我将其传递到server.js文件时,我没有收到警报的输出:
Meteor.methods({
'addToProfile': function(options) {
//Not even this alert will show..
alert(options._id); Edit: console.log() works on the server thought.
}
})
这是我的第一个问题
第二个问题(即将出现)是,我不知道如何使用此profilePicture数据更新/添加到users集合。如果有人能提供该部分的一个小例子,我将不胜感激。根据评论,一切似乎都如预期的那样运行。看起来您只是试图更新客户端上的一些用户数据。由于删除了不安全的
包,您需要验证服务器上的更新(客户端请求的更新),您可以这样做:
// only applies to client requests
Meteor.users.allow({
// validation for user inserts
insert: function ( userId, doc ) {
// perform any checks on data that you want to perform, like checking to see if the current user is an admin.
// this check just requests that a user be logged in
return userId;
}
,
// is your validation for user updates
update: function ( userId, doc, fields, modifier ) {
// again, perform any validation you want. A typical check is to make sure they didn't add any extra fields.
// this makes sure a user is logged in and that they are only attempting to update themself
return userId === doc._id;
}
});
在本文中有一些更全面的例子。现在,您可以像平常一样调用update,并依靠服务器执行任何验证 alert()是服务器上的函数吗?我想您可能需要console.log(options.\u id)
。是的,我认为警报不是服务器端的功能。您可以尝试consoloe.log()
,最终可以看到日志。您可以使用Meteor.user.update
示例Meteor.users.update({id:Meteor.user().\u id},{$set:{profile.name:“XXXX”})更新用户coll
我确实得到了console.log()来给我一些输出,但不是在chrome控制台中,而是在终端中…:)@getJETsetTER Yea,这些方法是在服务器上评估的,在服务器上,console.log
将参数打印到终端,因此一切都按预期进行。但是你并不需要一个方法来更新集合中的文档,只要按照iAmME(某种程度上)写的那样做就行了。
// only applies to client requests
Meteor.users.allow({
// validation for user inserts
insert: function ( userId, doc ) {
// perform any checks on data that you want to perform, like checking to see if the current user is an admin.
// this check just requests that a user be logged in
return userId;
}
,
// is your validation for user updates
update: function ( userId, doc, fields, modifier ) {
// again, perform any validation you want. A typical check is to make sure they didn't add any extra fields.
// this makes sure a user is logged in and that they are only attempting to update themself
return userId === doc._id;
}
});