gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 01:29:33 26 4
gpt4 key购买 nike

我正在尝试找出从填写为一个逗号分隔字符串的表单字段中传递值的最佳方法,但看起来我在尝试传递多个值时遇到了错误。有没有解决这个问题或者可能对列使用 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com