续集BulkCreate错误

我想弄清楚如何使用我的jQuery字段值的bulkCreate方法。 目前我正在尝试传递数组中的字段的值,并期望每个值被parsing到一个新的logging,但它不会出现像这样的方式工作,因此我收到一个错误:

Error during Post: SequelizeDatabaseError: ER_PARSE_ERROR: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '['Test 1','Test 2','Test 3'],'6','2016-02-01 03:26:59','2016-02-01 03:26:59')' at line 1 

这是因为我应该实际应用路线内的逻辑来处理不同的值? 我应该改变了jQuery?

查看文件(discoveryName是bulkCreate的字段):

 <!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">Test 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</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> 

路线:

 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/discovery-source') .get(function(req, res){ models.Organization.find({ where: { organizationId: req.user.organizationId }, attributes: ['organizationId'] }).then(function(organization){ res.render('pages/app/sign-up-discovery-source.hbs',{ organization: organization }); }); }) .post(function(req, res){ models.DiscoverySource.bulkCreate([ { discoverySource: req.body.discoverySource, organizationId: req.body.organizationId } ]).then(function(){ return models.DiscoverySource.findAll(); }).then(function(discoverySource){ console.log(discoverySource); res.redirect('/app'); }).catch(function(error){ res.send(error); console.log('Error during Post: ' + error); }); }); 

DiscoverySource模型字段:

  discoverySourceId: { type: DataTypes.INTEGER, field: 'discovery_source_id', autoIncrement: true, primaryKey: true }, discoverySource: { type: DataTypes.STRING, field: 'discovery_source' }, organizationId: { type: DataTypes.TEXT, field: 'organization_id' }, 

使用.bulkCreate你需要传递一个将被转换成行的对象数组。

 .post(function(req, res){ var sources = _.map(req.body.discoverySource, function (source) { return { discoverySource: source, organizationId: req.body.organizationId }; }); models.DiscoverySource.bulkCreate(sources) .then(function(){ return models.DiscoverySource.findAll(); }).then(function(discoverySource){ console.log(discoverySource); res.redirect('/app'); }).catch(function(error){ res.send(error); console.log('Error during Post: ' + error); }); });