2015-09-28 125 views
1

正要槽流星教程,並希望引進的添加,更新方法和刪除功能,所以,我不是直接從客戶端得到與Collection.allow做了書面許可,否則...它與方法,我一直運行到流星MongoDB的更新的RangeError

RangeError: Maximum call stack size exceeded 
    at Object.toString (native) 
    at isArguments (http://localhost:3000/packages/es5-shim.js?03b82f907286b5353e3a371eb320635a634fc88b:988:12) 
    at Function.keys (http://localhost:3000/packages/es5-shim.js?03b82f907286b5353e3a371eb320635a634fc88b:1051:13) 
    at Function._.each._.forEach (http://localhost:3000/packages/underscore.js?46eaedbdeb6e71c82af1b16f51c7da4127d6f285:155:20) 
    at Object.EJSON.clone (http://localhost:3000/packages/ejson.js?5e95dd4b5971d96cb2d3287c54b14d9002f83ab7:528:5) 
    at http://localhost:3000/packages/ejson.js?5e95dd4b5971d96cb2d3287c54b14d9002f83ab7:529:22 
    at Function._.each._.forEach (http://localhost:3000/packages/underscore.js?46eaedbdeb6e71c82af1b16f51c7da4127d6f285:157:22) 
    at Object.EJSON.clone (http://localhost:3000/packages/ejson.js?5e95dd4b5971d96cb2d3287c54b14d9002f83ab7:528:5) 
    at http://localhost:3000/packages/ejson.js?5e95dd4b5971d96cb2d3287c54b14d9002f83ab7:529:22 
    at Function._.each._.forEach (http://localhost:3000/packages/underscore.js?46eaedbdeb6e71c82af1b16f51c7da4127d6f285:157:22) 

所以這是我的代碼:

方法

Meteor.methods({ 
    addParty: function (party) { 
    // Make sure the user is logged in before inserting a task 
    if (!Meteor.userId()) { 
     throw new Meteor.Error('not-authenticated'); 
    } 

    party.owner = Meteor.userId(); 
    party.username = Meteor.user().username; 
    party.createdAt = new Date(); 

    Parties.insert(party); 
    }, 
    removeParty: function (party) { 
    // Make sure only the party owner can delete a party 
    if (party.owner !== Meteor.userId()) { 
     throw new Meteor.Error('not-authorized'); 
    } 
    Parties.remove(party._id); 
    }, 
    updateParty: function (party) { 
    // Make sure only the party owner can delete a party 
    if (party.owner !== Meteor.userId()) { 
     throw new Meteor.Error('not-authorized'); 
    } 
    Parties.update(party._id, party); 
    } 
}); 

UI: 在這裏你會看到和更改黨的細節:

<input ng-model="party.name"> 
<input ng-model="party.description"> 
<label>Is public</label> 
<input type="checkbox" ng-model="party.public"> 

<button ng-click="save(party)">Save</button> 
<button ng-click="reset()">Reset form</button> 
<button ui-sref="parties">Cancel</button> 

控制器:

$scope.party = $meteor.object(Parties, $stateParams.partyId, false); 

$scope.save = function(updatedParty) { 
    $meteor.call('updateParty', updatedParty); 
}; 

但是這是一個集合時做的工作:

$scope.addParty = function(newParty){ 
    $meteor.call('addParty', newParty); 
    } 

    $scope.remove = function(party){ 
    $meteor.call('removeParty', party); 
    } 

當我在教程中這樣做時,從客戶端調用它,它可以工作,但我希望將其作爲方法並更新此文檔中的所有字段。我也嘗試刪除Meteor.methods.updateParty中的所有內容,或者使用$ set,仍然會出現錯誤。沒有什麼似乎工作,我嘗試。有人看到問題在哪裏嗎?

謝謝

更新2015年10月29日。 17:51

好吧,當我改變了黨的從接收:

$scope.party = $meteor.object(Parties, $stateParams.partyId, false).subscribe('parties'); 

到:

$scope.party = Parties.findOne($stateParams.partyId); 

那麼方法調用的作品。但現在不行的是,當我更新頁面時,聚會不會再次被提取,就在我第一次來的時候。任何提示?

現在的問題是,這是做正確的方式。

我應該用Parties.findOne取它...或者是罰款$ meteor.object去取和定義,我可以從雙方的客戶寫允許:

Parties.allow({ 
    update: function (userId, party, fields, modifier) { 
    return userId && party.owner === userId; 
    } 
}); 
+0

不確定,因爲我不使用角度,但我認爲有一個無限循環崩潰堆棧。它可以發生在像雙角綁定系統中。 – acemtp

+0

@acemtp,謝謝你的評論。好的,什麼樣的無限循環?我更新了它,當我使用$ meteor.object ..與締約方.Allow,然後它的工作,但後來我從客戶端寫入數據庫,不知道這是否是正確的方式做... ..但在教程這是一個使用.. – damir

回答

1

所以,我有一個工作解決方案。

我創建模型(客戶端和服務器之間共享的碼)的方法,從DB檢索一方:

getOneParty: function(partyId) { 
    var party = Parties.findOne(partyId); 
    return party; 
    }, 

然後在客戶端我有此:

$meteor.subscribe('parties'); 
$scope.partyId = $stateParams.partyId; // getting this data from params 
$party = $meteor.call('getOneParty', $scope.getReactively('partyId')).then(
     function(data){ 
     $scope.party = data; 
     console.log('successfully got Party: ', data); 
     }, 
     function(err){ 
     console.log('failed', err); 
     } 
    ); 

然後我我可以稱這個功能沒有任何問題從用戶界面:

$scope.save = function(updatedParty) { 
     $meteor.call('updateParty', updatedParty); 
    }; 

所以,仍然不確定是否下面的其他方式是更好的,但至少我現在有兩個版本工作:)不知道關於下面的一個,因爲客戶然後直接寫入數據庫,這不是很安全?

客戶端代碼:

$meteor.subscribe('parties'); 
$scope.party = $meteor.object(Parties, $stateParams.partyId, false); 

更新方法:

$scope.save = function(updatedParty) { 
     $scope.party.save().then(function(numberOfDocs){ 
     console.log('save success doc affected ', numberOfDocs); 
     }, function(error){ 
     console.log('save error', error); 
     }); 
    }; 

,但對於這一點,就必須賦予權限的客戶這樣做。你可以做的是,在模型:

Parties.allow({ 
    update: function (userId, party, fields, modifier) { 
    return userId && party.owner === userId; 
    } 
}); 
0

試試這個:

$scope.$meteorSubscribe('parties', $stateParams.partyId).then(function() { 
     $scope.party= s.$meteorObject(Parties, { 
      _id: $stateParams.partyId 
     }, false); 
    }, function(data) { 
     alert("Error loading "); 
     console.log(data); 
    }) 

我有同樣的問題做它自己的方式,真的不知道爲什麼。出於某種原因,這爲我修好了。