Search code examples
mongodbmeteor-accountsmeteor-autoformmeteor-collection2simple-schema

insert meteor.userId into mongoDB


I want to initialize a field in simpleSchema. I want set userId into a string field.

here is the schema:

AdsSchema = new SimpleSchema({
  title: {
    type: String,
    label: "Title"
  },
  insertedBy: {
    type: String,
    label: "Inserted By",
    autoform:{
      type:"hidden"
    },
    autoValue: function() {
      if (this.userId) {
        return this.userId;
      }
    }
  }
}

but after inserting by autoform, the inserted document is:

{
    "_id" : "Zm5TML5bwJhyN8B9F",
    "title" : "test"
}

it means "insertedBy" field has "null" value!

how can I fix this?


Solution

  • Try with if(Meteor.userId()) return Meteor.userId()

    this in autoValue has limited Properties, check the Doc Autoform Docs

    Gave a little study on autoValue , Under cleaning Data there is a mention of property extendAutoValueContext which helps to use userId .