Sequelize验证错误字符串违规:

时间:2016-01-23 22:38:24

标签: sequelize.js

我试图找出从填写为逗号分隔字符串的表单字段传递值的最佳方法,但看起来我在尝试传递多个值时遇到错误。有没有相关的或可能使用SET数据类型的列?

discoverySource是我所说的领域

这是错误:

{"name":"SequelizeValidationError","message":"string violation: discoverySource cannot be an array or an object","errors":[{"message":"discoverySource cannot be an array or an object","type":"string violation","path":"discoverySource","value":["NJ","BK","Somewhere?"]}]}

型号:

module.exports = function(sequelize, DataTypes) {

var Organization = sequelize.define('organization', {
    organizationId: {
        type: DataTypes.INTEGER,
        field: 'organization_id',
        autoIncrement: true,
        primaryKey: true
    },
    organizationName: {
        type: DataTypes.STRING,
        field: 'organization_name'
    },
    admin: DataTypes.STRING,
    discoverySource: {
        type: DataTypes.STRING,
        field: 'discovery_source'
    },
    members: DataTypes.STRING
},{
    freezeTableName: true,
    classMethods: {
        associate: function(db) {
            Organization.belongsToMany(db.User, { through: 'member', foreignKey: 'user_id' });
        },
    },
});

    return Organization;
}

表单视图:

<!DOCTYPE html>
<head>
    {{> head}}
</head>
<body>
    {{> navigation}}
    <div class="container">
        <div class="col-md-6 col-md-offset-3">
            <form action="/app/sign-up/organization" method="post">
                <p>{{user.email}}</p>
                <input type="hidden" name="admin" value="{{user.email}}">
                <input type="hidden" name="organizationId">
                <label for="sign-up-organization">Company/Organization Name</label>
                <input type="text" class="form-control" id="sign-up-organization"  name="organizationName" value="" placeholder="Company/Organization">
                <a href="#" id="sign-up-add-discovery-source">Add Another Discovery Source</a>
                <div id="sign-up-organization-discovery-source">
                    <input type="text" id="discovery-source-field" placeholder="Discovery Source" name="discoverySource[0]">
                </div>
                <br />
                    <button type="submit">Submit</button>
            </form>
            <a href="/login">Already have an account? Login here!</a>
        </div>
    </div>
    <script type="text/javascript">
        $(function() {
  var dataSourceField = $('#sign-up-organization-discovery-source');
  var i = $('#sign-up-organization-discovery-source p').size();
  var sourceCounter = 1;

  $('#sign-up-add-discovery-source').on('click', function() {
    $('<p><label for="discovery-source-field"><input type="text" id="discovery-source-field" size="20" name="discoverySource['+ sourceCounter++ +']" value="" placeholder="Discovery Source" /></label> <a href="#" class="remove">Remove</a></p>').appendTo(dataSourceField);
    i++;
    return false;
  });
  $('#sign-up-organization-discovery-source').on('click', '.remove', function() {
    if (i > 1) {
      $(this).parent('p').remove();
      i--;
    }
    return false;
  });
});

    </script>
</body>

路线:

routes.route('/sign-up/organization')

    .get(function(req, res){
        models.User.find({
            where: {
                user_id: req.user.email
            }, attributes: [ 'user_id', 'email'
            ]
        }).then(function(user){
            res.render('pages/app/sign-up-organization.hbs',{
                user: req.user
            });
        })  
    })

    .post(function(req, res, user){
        models.Organization.create({
            organizationName: req.body.organizationName,
            admin: req.body.admin,
            discoverySource: req.body.discoverySource
        }).then(function(organization, user){

            models.Member.create({
                organizationId: organization.organizationId,
                memberEmail: req.user.email,
                userId: req.user.user_id
            },{ where: { user_id: req.user.user_id }});
            return organization;

        }).then(function(organization, user){
            models.User.update({
                organizationId: organization.organizationId
            },{ where: { user_id: req.user.user_id }});
            res.redirect('/app');
        }).catch(function(error){
            res.send(error);
            console.log('Error at Post' + error);
        })
    });

2 个答案:

答案 0 :(得分:1)

如果req.body.discoverySource有值 ["NJ","BK","Somewhere?"]并且您希望将"NJ,BK,Somewhere?"存储在discoverySource列中,请使用

req.body.discoverySource.join(',')

答案 1 :(得分:0)

您也可以使用 JSON.stringify type: Sequelize.STRING

JSON.stringify(req.body.discoverySource)