2014-01-17 3 views
7

コレクションにアップグレードしようとしたときにコンソールでこのエラーが発生する:流星はアップセートを許可しますか?

"更新に失敗しました:アクセスが拒否されました。制限付きコレクションで許可されていません。ここで

は私が指定した許可ルールです:ここでは

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); 
}; 

答えて

8

これはa choice the development teamで作られています。

提案されている解決策は、upsertをラップするメソッドを記述することです。これにより、サーバコードはサーバコードから取得され、クライアントコードはレイテンシ補正のためにのみ実行されます。例:

//shared code 
Meteor.methods({ 
    customersUpsert: function(id, doc){ 
    Customers.upsert(id, doc); 
    } 
}); 

//called from client 
Meteor.call('customersUpsert', Customers.maybeFindOne(attr)._id, attr); 
+0

ありがとうございます!それがトリックでした。私はまた、挿入と更新を分割し、$ setを使うことに成功しました。 – user3203772

1

これは、回避策、私は(アンダースコアのデフォルト機能を使用して)使用している:selectorがオブジェクトIDであることを前提としてい

_upsert: function(selector, document) { 
    if (this.collection.findOne(selector) != null) { 
    this.collection.update(selector, { 
     $set: document 
    }); 
    } else { 
    this.collection.insert(_.defaults({ 
     _id: selector 
    }, document)); 
    } 
} 

関連する問題