【问题标题】:Sequelize BulkCreate ErrorSequelize BulkCreate 错误
【发布时间】:2016-02-02 02:57:19
【问题描述】:

我试图弄清楚如何将 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'
    },

【问题讨论】:

    标签: jquery node.js express sequelize.js


    【解决方案1】:

    使用.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);
        });
    });
    

    【讨论】:

    • 嘿,Ben,我很欣赏这个答案,但似乎 return 语句在 return { discoverySource: src, organizationId: req.body, organizationId }; SyntaxError: Unexpected token } 处引发了语法错误。任何想法为什么会这样?
    • 您介意在我的.post 路线的上下文中写下这个答案,以便我正确实施吗?
    • 我很欣赏编辑,但现在$ 似乎遇到了错误。 ReferenceError: $ is not defined 有什么想法吗?
    • @cphill 这通常是访问 jQuery 的方式。我不确定你的应用程序是如何设置的,但由于它看起来像 Node,我可能会引入 loDash。 var _ = 要求('lodash');那么您可以使用相同的代码,只需替换 _.each()
    • 嘿,本,这解决了我的问题,但似乎没有在函数中传递值。 post 方法插入记录表单中存在的字段数量,但我得到的值为 NULL。 console.log 中的值是从req.body.discoverySource 传递的值,但它似乎没有将该值传递给变量。我应该为此提出一个新问题吗? ['测试源'] Executing (default): INSERT INTO discovery_source` (discovery_source_id,createdAt,updatedAt) 值 (NULL,'2016-02-07 20:29:24','2016-02-07 20:29 :24');`
    猜你喜欢
    • 2021-01-28
    • 2018-10-05
    • 1970-01-01
    • 2016-05-06
    • 2019-11-28
    • 2021-11-02
    • 2019-11-20
    • 2018-08-10
    • 1970-01-01
    相关资源
    最近更新 更多