【问题标题】:Passing boolean through Waterline (Sailsjs)?通过水线(Sailsjs)传递布尔值?
【发布时间】:2023-04-09 01:28:01
【问题描述】:

我有一个问题案例测试 HTML 输入的真实性,然后通过 ORM 获取真假案例以保存:

open_to_public: (req.param('open_to_public') == 't' ? true: false),

Waterline 文档说字符串将无法验证,所以我直接设置布尔值,如上所述。但是,该模型无法验证这一点,结果如下:

{ [error: invalid input syntax for type boolean: ""]
name: 'error',
length: 83,
severity: 'ERROR',
code: '22P02',
detail: undefined,
hint: undefined,
position: undefined,
internalPosition: undefined,
internalQuery: undefined,
where: undefined,
file: 'bool.c',
line: '153',
routine: 'boolin' }

我尝试了任意数量的大小写、文字或引用值的组合,并多次调整我的模型。后端是 MySQL,列的类型是布尔值。列中现有数据均表示为 TRUE/FALSE。

这是当前的匹配模型定义(导致上面列出的错误):

open_to_public: {
  type: 'boolean',
  defaultsTo: 'false',
  boolean: true,
},

感谢任何见解。

编辑:按照 Scott 的要求:在 LeaseController.js 中

create: function(req,res,next) {

    if( req.param('client_id') == null || req.param('rental_type_id') == null ) {
        console.log( 'Minimum requirements missing' );
        return next();
    }

    var data = {
        client_id: req.param('client_id'),
        rental_type_id: req.param('rental_type_id'),
        clerk: req.session.user.login,
        site_contact_person: req.param('site_contact_person'),
        site_contact_phone: req.param('site_contact_phone'),
        open_to_public: (req.param('open_to_public') == 't' ? true: false),
        admission_fee: (req.param('admission_fee') ? req.param('admission_fee'): 0.00),
        rental_confirmation: req.param('rental_confirmation'),
        require_deposit: (req.param('require_deposit')?req.param('require_deposit'):0.00)
    };

    Lease.create( data ).done(function(err,lease){
        if( err ) {

    ///// THIS IS WHERE THE ERR IS THROWN //////
            console.log( err );
            console.log( data );
            return next();
        }
        if( ! lease ) {
            console.log( 'No lease object returned from create function' );
            return next();  
        }

        req.flash('success', 'Lease '+ lease.id + ' created');

        res.redirect( '/lease/'+lease.id, {data:lease} );
        return;
    });

}

【问题讨论】:

  • 你能显示给你错误的代码吗?
  • 您确定是 open_to_public 字段导致了这种情况吗?你还有其他布尔字段吗?

标签: javascript mysql sails.js waterline


【解决方案1】:

你可以试试这样的:

open_to_public: (req.param('open_to_public') == 't' ? 'true': 'false'),

【讨论】:

    【解决方案2】:

    您可以尝试更改模型定义

    open_to_public: {
      type: 'boolean',
      defaultsTo: false
    },
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-05-28
      • 2017-08-19
      • 2014-07-26
      • 2016-08-06
      • 1970-01-01
      • 2016-12-31
      • 2017-07-02
      相关资源
      最近更新 更多