Collections 流星允许上升?
尝试升级到集合时在控制台中获取此错误: “更新失败:访问被拒绝。在受限集合中不允许升级。” 以下是我指定的允许规则:Collections 流星允许上升?,collections,meteor,upsert,Collections,Meteor,Upsert,尝试升级到集合时在控制台中获取此错误: “更新失败:访问被拒绝。在受限集合中不允许升级。” 以下是我指定的允许规则: if (Meteor.isClient) { Meteor.subscribe('customers'); } customers = Customers if (Meteor.isServer) { Meteor.publish('customers', function() { return customers.find(); }); customer
if (Meteor.isClient) {
Meteor.subscribe('customers');
}
customers = Customers
if (Meteor.isServer) {
Meteor.publish('customers', function() {
return customers.find();
});
customers.allow({
insert: function (document) {
return true;
},
update: function () {
return true;
},
remove: function () {
return true;
}
});
}
下面是插入部分:
Customer.prototype.create = function ( name, address, phone, cell, email, website, contact, shipping ) {
var attr = {
name : name,
address : address,
phone : phone,
cell : cell,
email : email,
website : website,
contact : contact,
shipping : shipping
};
Customers.upsert( Customers.maybeFindOne( attr )._id, attr );
return new Customer( attr );
};
这是我做的
建议的解决方案是编写一个包装upsert的方法。这使得服务器请求来自服务器代码,而客户端代码仅用于延迟补偿。例如:
//shared code
Meteor.methods({
customersUpsert: function( id, doc ){
Customers.upsert( id, doc );
}
});
//called from client
Meteor.call( 'customersUpsert', Customers.maybeFindOne( attr )._id, attr );
这是我使用的解决方法(使用下划线的默认值函数):
假设选择器是一个对象ID。谢谢!这就成功了。我还成功地拆分了插入和更新,并使用了$set。
_upsert: function(selector, document) {
if (this.collection.findOne(selector) != null) {
this.collection.update(selector, {
$set: document
});
} else {
this.collection.insert(_.defaults({
_id: selector
}, document));
}
}