簡體   English   中英

如何通過autoValue將Meteor.userId()添加到SimpleSchema?

[英]How to add Meteor.userId() to SimpleSchema via autoValue?

在我的libs文件夾中,我使用SimpleSchema創建集合。 我想通過autoValue將Meteor.userId添加到某些字段,如下所示:

Collection = new Meteor.Collection('collection');
Collection.attachSchema(new SimpleSchema({
    createdByUser: {
        type: String,
        max: 20,
        autoValue: function() {
            return Meteor.userId();
        }
    }
});

但是,當這樣做時,我收到以下錯誤:

Error: Meteor.userId can only be invoked in method calls. Use this.userId in publish functions.

我也嘗試過這個:

var userIdentification = Meteor.userId();
Collection = new Meteor.Collection('collection');
Collection.attachSchema(new SimpleSchema({
    createdByUser: {
        type: String,
        max: 20,
        autoValue: function() {
            return userIdentification;
        }
    }
});

這會使我的應用程序崩潰:

=> Exited with code: 8
=> Your application is crashing. Waiting for file change.

有任何想法嗎?

userId信息collection2通過this提供給autoValue

autoValue選項由SimpleSchema包提供,並在那里記錄。 對於作為C2數據庫操作的一部分調用的任何autoValue函數,Collection2為此添加以下屬性:

  • isInsert:如果是插入操作,則為True
  • isUpdate:如果是更新操作,則為True
  • isUpsert:如果是upsert操作(upsert()或upsert:true),則為true
  • userId:當前登錄用戶的ID。 (對於服務器啟動的操作,始終為null。)

所以你的代碼應該是:

Collection = new Meteor.Collection('collection');
Collection.attachSchema(new SimpleSchema({
    createdByUser: {
        type: String,
        max: 20,
        autoValue: function() {
            return this.userId;
        }
    }
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM