SailsJS:神秘的空值

时间:2017-12-06 00:03:19

标签: node.js sails.js

我的问题是,每当创建一个新客户端时,除了id,name,Responsable和logo之外,数据库中的所有值都为null。我不认为我做了一个程序错误,所以我认为这是一个回调比赛的情况,但我找不到解决方案。 P.S:仅当我选择并发送要上载的图像文件时才会出现问题,在另一种情况下,客户端值被正确存储。 P.S 2:仅在我的远程服务器中出现问题,在本地环境中一切正常!

比你多!

更新:我包含了我的create.ejs视图的代码

这是我的ClientService中的store方法的代码:

store: function(req, done) {
  var name = req.param('name'),
    town = req.param('town'),
    adress = req.param('adress'),
    postalCode = req.param('postalCode'),
    telephone = req.param('telephone'),
    email = req.param('email'),
    fax = req.param('fax'),
    responsable = req.param('responsable'),
    website = req.param('website'),
    activity = req.param('activity');
  comments = req.param('comments');

  Client.create({
    name: name,
    town: town,
    adress: adress,
    postalCode: postalCode,
    telephone: telephone,
    fax: fax,
    responsable: responsable,
    website: website,
    activity: activity,
    email: email,
    comments: comments
  }).exec(function(err, client) {
    if (err) console.log(err);
    req.file('logo').upload(
      {
        dirname: sails.config.appPath + sails.config.params.logos
      },
      function(err, logo) {
        if (err) return done(err, null);
        if (logo.length !== 0) {
          client.logo = require('path').basename(logo[0].fd);
        } else {
          client.logo = 'default.png';
        }
        client.save(function(err) {
          return done(null, client);
        });
      }
    );
  });
}

这是EJS视图的代码:

<div class="portlet-body form">
                        <!-- BEGIN FORM-->
                        <form action="store" method="POST" class="form-horizontal" enctype="multipart/form-data">
                            <div class="form-body">
                                <div class="row">
                                    <div class="col-md-6">
                                        <div class="form-group form-md-line-input has-success form-md-floating-label form-create">
                                            <div class="input-icon">
                                                <input type="text" class="form-control" name="name">
                                                <label for="form_control_1">Nom du Client</label>
                                                <i class="fa fa-institution"></i>
                                            </div>
                                        </div>
                                    </div>
                                    <div class="col-md-6">
                                        <div class="form-group form-md-line-input has-success form-md-floating-label form-create">
                                            <div class="input-icon">
                                                <input type="text" class="form-control" name="activity">
                                                <label for="form_control_1">Activité</label>
                                                <i class="icon-star"></i>
                                            </div>
                                        </div>
                                    </div>
                                    </div>
                                <div class="row">
                                    <div class="col-md-6">
                                         <div class="form-group form-md-line-input has-success form-md-floating-label form-create">
                                            <div class="input-icon">
                                                <input type="text" class="form-control" name="responsable">
                                                <label for="form_control_1">Responsable</label>
                                                <i class="icon-user"></i>
                                            </div>
                                        </div>
                                    </div>
                                    <div class="col-md-6">
                                     <div class="form-group" style="margin-left:15px;">
                                        <div class="form-photo-label-form" >
                                                &nbsp;&nbsp;<i class="icon-picture icon-create"></i>&nbsp;&nbsp; 
                                                <label for="form_control_1" class="form-photo-create" >Photo </label>
                                        </div>
                                        <br>
                                        <div class="fileinput fileinput-new" data-provides="fileinput">
                                                    <span class="btn green btn-file">
                                                    <span class="fileinput-new"> Selectionner Fichier </span>
                                                    <span class="fileinput-exists"> Changer </span>
                                                    <input type="file" name="logo"> </span>
                                                    <span class="fileinput-filename"> </span> &nbsp;
                                                     <a href="javascript:;" class="close fileinput-exists" data-dismiss="fileinput"> </a>
                                        </div>
                                    </div>
                                    </div>
                                </div>
                                <div class="row">
                                    <div class="col-md-6">
                                         <div class="form-group form-md-line-input has-success form-md-floating-label form-create">
                                            <div class="input-icon">
                                                <input type="text" class="form-control" name="email">
                                                <label for="form_control_1">Email</label>
                                                <i class="fa fa-inbox"></i>
                                            </div>
                                        </div>
                                    </div>
                                    <div class="col-md-6">
                                         <div class="form-group form-md-line-input has-success form-md-floating-label form-create">
                                            <div class="input-icon">
                                                <input type="text" class="form-control" name="adress">
                                                <label for="form_control_1">Adresse</label>
                                                <i class="icon-home"></i>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                                <div class="row">
                                     <div class="col-md-6">
                                        <div class="form-group form-md-line-input has-success form-md-floating-label form-create">
                                            <div class="input-icon">
                                                <input type="text" class="form-control" name="postalCode">
                                                <label for="form_control_1">Code postale</label>
                                                <i class="fa fa-send"></i>
                                            </div>
                                        </div>
                                    </div>
                                    <div class="col-md-6">
                                        <div class="form-group form-md-line-input has-success form-md-floating-label form-create">
                                            <div class="input-icon">
                                                <input type="text" class="form-control" name="town">
                                                <label for="form_control_1">Ville</label>
                                                <i class=" fa fa-map"></i>
                                            </div>
                                        </div>
                                    </div>
                                    <!--/span-->
                                </div>
                                <div class="row">
                                     <div class="col-md-6">
                                        <div class="row">
                                            <div class="col-md-12">
                                                <div class="form-group form-md-line-input has-success form-md-floating-label form-create">
                                                <div class="input-icon">
                                                <textarea class="form-control" rows="3" style="height: 192px; resize:none " name="comments"></textarea>
                                                <label for="form_control_1">Commentaire</label>
                                                <i class=" fa fa-edit"></i>
                                                </div>
                                            </div>
                                            </div>
                                        </div>
                                     </div>
                                     <div class="col-md-6">
                                        <div class="row">
                                            <div class="col-md-12">
                                                <div class="form-group form-md-line-input has-success form-md-floating-label form-create">
                                                    <div class="input-icon">
                                                        <input type="text" class="form-control" name="telephone">
                                                        <label for="form_control_1">Telephone</label>
                                                        <i class="icon-screen-smartphone"></i>
                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                        <div class="row">
                                            <div class="col-md-12">
                                                <div class="form-group form-md-line-input has-success form-md-floating-label form-create">
                                                    <div class="input-icon">
                                                        <input type="text" class="form-control" name="fax">
                                                        <label for="form_control_1">Fax</label>
                                                    <i class="fa fa-fax"></i>
                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                        <div class="row">
                                            <div class="col-md-12">
                                                <div class="form-group form-md-line-input has-success form-md-floating-label form-create">
                                                    <div class="input-icon">
                                                        <input type="text" class="form-control" name="website">
                                                        <label for="form_control_1">Site Internet</label>
                                                        <i class=" fa fa-internet-explorer"></i>
                                                     </div>
                                                </div>
                                            </div>
                                        </div>
                                    </div>

                                </div>
                            </div>

                    <div class="form-actions right">
                        <button type="button" class="btn default">Annuler</button>
                        <button type="submit" class="btn green"><i class="fa fa-check"></i> Enregistrer</button>
                    </div>
                </form>

2 个答案:

答案 0 :(得分:2)

没什么神秘的(: 您的问题是您使用的是req.param而不是req.body

你的代码应该是这样的:

const path = require('path'); // somewhere in same module at the top

store: (req, done) => {

  Client
    .create(req.body)
    .exec((err, client) => {
      if(err) {
        // no need to go further 
        // if You cannot create database record
        return done(err);
      } 

      const dirname = path.join(sails.config.appPath, sails.config.params.logos); // safely concatenates paths based on OS

      req
        .file('logo')
        .upload({dirname}, (err, logo) => {
          if (err) {
            // we created record (client) 
            // but could not save the file
            // it should not be a stopper
            console.error(err);
          }

          client.logo = (logo) ? path.basename(logo[0].fd) : 'default.png';

          client.save((err) => {
            if(err) {
              // just log the error and continue
              console.error(err);
            }
            done(null, client);
          });
        });
    });
}



附:当您将req.body(或任何其他)对象传递给Client.create并不担心对象内容时,只需在您的模型文件中定义字段约束,ODM(或ORM)将自动处理验证关于约束并将阻止创建空值字段

示例:

module.exports = {

  attributes: {

    name: {
      // it requires field name:
      //  to be defined (required: true), 
      //  to be string (type), 
      //  to have at least 2 symbols,
      //  to not exceed 100 symbols
      type: 'string',
      required: true,
      minLength: 2,
      maxLength: 100
    },

    email: {
      // it requires field email:
      //  to be defined (required: true), 
      //  to be email (type), 
      //  to be unique among documents, records, rows
      type: 'email',
      required: true,
      unique: true
    },
    ... and so on ...
  }
}

有关验证的更多信息here

答案 1 :(得分:1)

谢谢大家。我通过将文件输入放在表单的末尾来解决问题。与您的建议一样,文件输入后的值在上传文件后重置