2015-09-28 126 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); 
    }) 

我有同样的问题做它自己的方式,真的不知道为什么。出于某种原因,这为我修好了。