我想弄清楚如何将 bulkCreate 方法与我的 jQuery 字段值一起使用。目前,我正在尝试传递数组中字段的值,并希望将每个值解析为一个新记录,但它看起来不像这种方法那样工作,因此我收到一个错误:

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

关于jquery - Sequelize BulkCreate 错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35144259/

10-12 00:12