gpt4 book ai didi

javascript - SequelizeJS 传递值数组

转载 作者:行者123 更新时间:2023-12-03 07:49:44 28 4
gpt4 key购买 nike

我试图弄清楚如何将数组作为实例属性的值传递。目前,我在模型中将 dataType 设置为 STRING,并将 jQuery 字段中的值插入每个表单字段值到我从正文中解析并设置为属性的数组中,发现源。不幸的是,我收到一个字符串违规错误,指出我无法使用数组或对象。这是什么意思?如何更改字段或路由的 dataType 以允许我将逗号分隔值传递到字段?

E.x.对于 discoverySource,我将值传递给两个字段(NJ、NY)。提交时,这些值会以 ["NJ", "NY"] 形式组合在一个数组中,并显示错误:

错误消息:

{"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","NY"]}]}

这是我的模型:

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.TEXT,
field: 'discovery_source'
},
members: DataTypes.STRING
},{
freezeTableName: true,
classMethods: {
associate: function(db) {
Organization.belongsToMany(db.User, { through: 'member', foreignKey: 'user_id' });
},
},
});
return Organization;
}

路线如下:

var express = require('express');
var appRoutes = express.Router();
var passport = require('passport');
var localStrategy = require('passport-local').Strategy;
var models = require('../models/db-index');

appRoutes.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){
res.redirect('/app');
}).catch(function(error){
res.send(error);
console.log('Error at Post' + error);
})
});

这是我的 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>

最佳答案

为了回答最后一条评论,我需要能够使代码更具可读性,因此我将其发布在新答案中。

再考虑一下,将其添加为自定义“getter”函数会更有意义。我还将添加“instanceMethods”来演示其工作原理。

var Organization = sequelize.define('organization', {
...
},{
freezeTableName: true,
classMethods: {
associate: function(db) {
Organization.belongsToMany(db.User, { through: 'member', foreignKey: 'user_id' });
},
},
// Here's where custom getters would go
getterMethods: {
discoverySources: function() {
return this.getDataValue('discoverySource');
}
},
// here's the instance methods
instanceMethods: {
getSourcesArray: function() {
return this.getDataValue('discoverySource');
}
}
});

这两个选项都将函数添加到模型创建的每个实例中。主要区别在于它们的访问方式。

organization.discoverySources; // -> ['s1', 's2', etc...]
organization.getSourcesArray(); // -> ['s1', 's2', etc...]

注意instanceMethod 上需要的附加()。这些作为实例的函数添加,getterMethods 作为属性添加。

setterMethods 的工作方式相同,允许您定义自定义 setter 。

希望能够澄清一些事情。

关于javascript - SequelizeJS 传递值数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35051290/

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