簡體   English   中英

更新后,Meteor用戶幫助程序掛起

[英]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添加到用戶的profile.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 真的很奇怪。 要查找用戶擁有的餐館,您應該在餐館集合上查找。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM