流星用户助手在更新后挂起
Meteor user helper hangs off after update
我有这个助手:
agreed: function (){
if (Meteor.users.findOne({_id: Meteor.userId(), profile: {agreedTermsOfUse: 'true'}}))
return true;
}
在我检查它的页面上,我有这个:
{{#unless agreed}}
agree form
{{else}}
Create item form.
{{list of item}}
{{/unless}}
到目前为止,一切顺利。用户注册后,他可以创建一个项目,并在项目列表中呈现
。现在,我添加了另一个 Meteor.call,当在客户端上获得成功调用时,对于创建项,它会将项 ID 添加到用户的配置文件.hasItems。
然后在该方法获得成功后,"除非"返回 false,我必须再次提交同意表格。
我错过了什么?谢谢。
"submit .create_restaurant": function (event) {
event.preventDefault();
var text = event.target.create_restaurant.value;
Meteor.call('CreateRest', Meteor.userId(), text, function(error, result){
if(error){
}else{
console.log(result, Meteor.userId());
Meteor.call('userRestaurants', result, Meteor.userId(), function (error, result) {
if (error) {
alert(123);
} else {
console.log(result);
}
})
}
}
);
event.target.create_restaurant.value = "";
}
方法:
'CreateRest': function(user_id, title) {
check(title, String);
check(user_id, String);
return callback = Restaurants.insert({
createdBy: user_id,
createdAt: new Date(),
title: title
});
},
'userRestaurants': function(rest_id, createdBy) {
var restId = checkHelper(rest_id, createdBy);
if(restId)
console.log(rest_id, createdBy);
{
var callback = Meteor.users.update(
createdBy,
{$addToSet: {'profile.hasRestaurants': restId}}
);
return callback;
}
}
我不知道
你为什么会看到你的行为,但我知道你还有其他问题需要先解决:)
你有一个巨大的安全漏洞 - 你正在将用户ID从客户端传递给方法。这意味着任何人都可以简单地打开浏览器控制台,并使用他们喜欢的任何用户ID作为所有者创建一个餐厅。相反,在方法中使用 this.userId 来获取调用方的 id。
为什么要往返服务器?只需让第一种方法更新客户端即可。
所以,像这样的东西(未经测试,在这里手写):
"submit .create_restaurant": function (event) {
event.preventDefault();
var text = event.target.create_restaurant.value;
Meteor.call('CreateRest',text, function(error, result){
if(error){
alert(123);
}else{
console.log(result);
}
});
event.target.create_restaurant.value = "";
}
和:
'CreateRest': function(user_id, title) {
check(title, String);
check(this.userId, String);
userId = this.userId;
Restaurants.insert({
createdBy: userId,
createdAt: new Date(),
title: title
}, function(err, restId) {
if (err) throw new Meteor.Error(err);
Meteor.users.update(
userId,
{$addToSet: {'profile.hasRestaurants': restId}},
function (err, res) {
if (err) throw new Meteor.Error(err);
return restId;
}
);
});
一旦正确实施,它可能会开始工作。如果没有,则问题与您发布的代码无关。
最后请注意,从架构的角度来看,您拥有profile.hasRestaurants真的很奇怪。要查找用户拥有的餐厅,您只需在餐厅集合上进行查找。
相关文章:
- JavaScript执行暂时挂起页面
- 是定义的操作系统睡眠/挂起期间setTimeout的行为
- Chrome浏览器“;挂起”;同时在循环中执行AJAX请求
- 更新不起作用
- fullcalendar在删除导致浏览器挂起的多个事件时速度较慢
- 应用将图表转换为图像的jqplot代码时Chrome挂起
- 如何在不使用Internet Explorer的情况下从未挂起的HTML文件执行程序
- MongoDB collection.find()查询挂起
- 使用子字符串方法时应用程序挂起的原因
- 如何在AngularJS中取消挂起的请求
- 从node.js应用程序调用Mongoose Model.save()时挂起
- Chrome,Safari在从本地存储读取时挂起,而Firefox很好
- IE 10 挂起 XMLHttpRequest 6 分钟,然后响应请求(使用 AngularJS 实现 CRSF)
- 挂起 JavaScript 键关闭事件
- HERE 映射 JS API v3 集群提供程序挂起,添加许多数据点
- Ajax 调用挂起在 jQuery 中
- writeln img src将数据提交到外部系统-Firefox和IE挂起
- React计时器挂起:00(状态未更新)
- 流星用户助手在更新后挂起
- 为什么更新进度条时浏览器挂起