流星用户助手在更新后挂起

Meteor user helper hangs off after update

本文关键字:更新 挂起 用户 流星      更新时间:2023-09-26

我有这个助手:

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;
    }
}
我不知道

你为什么会看到你的行为,但我知道你还有其他问题需要先解决:)

  1. 你有一个巨大的安全漏洞 - 你正在将用户ID从客户端传递给方法。这意味着任何人都可以简单地打开浏览器控制台,并使用他们喜欢的任何用户ID作为所有者创建一个餐厅。相反,在方法中使用 this.userId 来获取调用方的 id。

  2. 为什么要往返服务器?只需让第一种方法更新客户端即可。

所以,像这样的东西(未经测试,在这里手写):

"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真的很奇怪。要查找用户拥有的餐厅,您只需在餐厅集合上进行查找。