sequelize.js - Sequelize验证错误字符串违规:

标签 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;
}

表单 View :

<!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);
        })
    });

最佳答案

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

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

关于sequelize.js - Sequelize验证错误字符串违规:,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34969809/

相关文章:

mysql - 如何在不使用 Sequelize 的情况下定义模型? (Node.JS 与 MySQL)

javascript - 来自另一个文件的模型返回值不适用于 Sequelize.js

sequelize.js - 一对多将对象与 sequelize 关联

mysql - 使用 Sequelize 仅从行和关联中获取值

mysql - 带有 Sequelize 和 Typescript 的 NodeJS

sequelize.Model.Update 的 Typescript 接口(interface)不正确?

node.js - Sequelize - 通过多重关联创建

node.js - sequelize 不包括 model.create 上的用户

node.js - 访问实例内的belongsToMany 关联

node.js - 如何在 Sequelize 中使用 between 运算符?