Meteor Upsert Restricted Collection 403错误

时间:2013-12-10 11:21:11

标签: meteor

我正在发现Meteor,并且我试图运行的集合upsert出了问题。我收到以下错误:

errorClass {error: 403, reason: "Access denied. Upserts not allowed in a restricted collection."…}

我真的不明白会发生什么,这是客户端代码:

pages = Pages.findOne({_id: Meteor.userId()});
PageId = (pages) ? pages._id : null;  // The PageId should be null in my situation
Pages.upsert(PageId, {$set: {"pages.facebook.url": "url"}}, {multi: false}, function(err, docs) {
    // callback code
});

和权限:(服务器端)

Pages = new Meteor.Collection('pages');

Pages.allow({
    insert: function(userId, doc) {
        return userId !== null;
    },
    update: function(userId, doc) {
        return userId == doc.userId;
    },
    remove: function(userId, doc) {
        return userId == doc.userId;
    }
});

2 个答案:

答案 0 :(得分:1)

从客户端更新时,无法将对象作为选择器传递,只能传递要更新的对象的单个_id。而不是

Pages.upsert({_id: PageId}, ...);

简单地写

Pages.upsert(PageId, ...);


编辑,在您的允许块中,您不会授予任何创建新对象的权限,因此操作将因访问受限而失败。将insert param添加到allow方法中。

答案 1 :(得分:0)

据我所知,我也面临着简单更新的问题以及集合定义中defaultValue的一些字段。

我最终得到了一个link,其中说有一个简单的黑客来解决这个问题。以下是代码,

const defaultValue = value => function autoValue() {
  if (!this.isUpdate && !this.isUpsert && !this.isSet) {
    return value;
  }
};  

// Schema field definition
fieldName: {
  type: Number,
  autoValue: defaultValue(10),
}

因此,从上面的代码可以看出,您必须将defaultValue : YOUR_VALUE替换为autoValue: defaultValue(YOUR_VALUE)

相关问题