ApostropheCMS与附件联系我们表格

时间:2018-11-20 15:21:27

标签: node.js apostrophe apostrophe-cms

我是Apostrophe的新手,并尝试按照本教程在Apostrophe中创建带有文件附件的“与我们联系”表单。 https://apostrophecms.org/docs/tutorials/intermediate/forms.html

我还在index.js中创建了附件字段,并且可以在管理面板中正常工作。

现在,我正在尝试为带有文件提交的表单创建自己的html。

//在lib / modules / contact-form-widgets / public / js / always.js中

apos.define('contact-form-widgets', {

    extend: 'apostrophe-widgets',

    construct: function(self, options) {

      self.play = function($widget, data, options) {

        var $form = $widget.find('[data-contact-form]');
        var schema = self.options.submitSchema;
        var piece = _.cloneDeep(self.options.piece);

        return apos.schemas.populate($form, self.schema, self.piece, function(err) {
          if (err) {
            alert('A problem occurred setting up the contact form.');
            return;
          }
          enableSubmit();
        });

        function enableSubmit() {
          $form.on('submit', function() {
            submit();
           //I can access file here
            // console.log($form.find('file'))

            return false;
          });
        }

        function submit() {
          return async.series([
             convert,
            submitToServer
          ], function(err) {
            if (err) {
              alert('Something was not right. Please review your submission.');
            } else {
              // Replace the form with its formerly hidden thank you message
              $form.replaceWith($form.find('[data-thank-you]'));
            }
          });
          function convert(callback) {
            return apos.schemas.convert($form, schema, piece, callback);
          }
          function submitToServer(callback) {



            return self.api('submit', piece, function(data) {

                alert("I AM AT SUBMIT API ")
              if (data.status === 'ok') {
                // All is well
                return callback(null);
              }
              // API-level error
              return callback('error');
            }, function(err) {
              // Transport-level error
                alert("I AM HERE AT API ERROR")

              return callback(err);
            });
          }
        }
      };
    }
  });



    //and my widget.html is

    <div class="form-group">
        <input name="custom-file" type="file">
      </div>

运行此命令时出现以下错误

user.js:310 Uncaught TypeError: Cannot read property 'serialize' of undefined
    at Object.self.getArea (user.js:310)
    at Object.self.getSingleton (user.js:303)
    at Object.convert (user.js:686)
    at user.js:164
    at async.js:181
    at iterate (async.js:262)
    at async.js:274
    at async.js:44
    at setImmediate.js:27
    at runIfPresent (setImmediate.js:46)

我的问题是,如何处理文件提交?有没有更好的方法呢?

1 个答案:

答案 0 :(得分:0)

使用apostrophe-pieces-submit-widgets模块可以轻松得多,该模块允许您为用户可以提交的内容定义模式。您可以在其中包含类型attachment的字段,这在自述文件中得到了证明。