【问题标题】:how to add extra attributes to the quickform before storing it in the database?如何在将其存储到数据库之前向快速表单添加额外的属性?
【发布时间】:2016-06-18 07:08:13
【问题描述】:

我有一个全局变量来存储用户上传图片的 url。 在将其添加到数据库之前,如何将该变量作为属性添加到文档中?

这是我的代码

Meteor.methods({
    submitPost: function (app) {
        // Console.log('new App:', app);
        check(app, {
            title: String,
            description: String,
            category: String,
            price: Number
        });
        Products.insert(app);
    }

});

我想在“app”中添加全局变量,然后再将其插入 Products 集合

我该怎么做?

这是我在收藏中添加的内容

previewImage: {
    type: String,
    autoValue: function(){
      return PIurl;
    },
    autoform: {
      type: "hidden"
    }
  },
  createdAt:{
    type: String,
    autoValue: function(){
      return new Date();
    },
    autoform: {
      type: "hidden"
    }
  }
}));

添加上述代码后,点击提交时没有任何反应,表单不再存储在数据库中

【问题讨论】:

    标签: meteor meteor-autoform


    【解决方案1】:

    有两种方法可以实现,第一种是使用AutoForm.hooksonSubmit钩子autoform hooks。另一种方法是使用 autoValue 的对象属性将其添加到您的架构中:

     Schema.something = new SimpleSchema({
         category: {
           type: String,
           autoValue: function () {
            return "foo";
           }
         },
    

    【讨论】:

    • 我添加了您的解决方案并编辑了我的问题,添加后当我单击提交时没有任何反应,现在正在检查自动生成挂钩:)
    • 你在使用meteor Autoform吗?
    • 是的,正在使用它,表单可以正常工作,无需添加 autoValue 字段,:)
    • 感谢它的工作,我把日期类型作为字符串,它应该是日期类型,当我改变它时,它有效,我接受了你的回答,谢谢:)
    猜你喜欢
    • 2015-12-19
    • 2013-04-19
    • 1970-01-01
    • 2021-12-05
    • 1970-01-01
    • 1970-01-01
    • 2017-12-26
    • 2013-11-20
    • 2013-03-30
    相关资源
    最近更新 更多