Sequelize BulkCreate错误

时间:2016-02-02 02:57:20

标签: jquery node.js express sequelize.js

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

<!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>

这是因为我应该在路由中应用循环逻辑来处理不同的值吗?我应该更改jQuery吗?

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

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

路线:

    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'
    },

DiscoverySource模型字段:

{{1}}

1 个答案:

答案 0 :(得分:2)

使用 Meteor.publishComposite('groupMembers', function () { return [{ find: function() { return Titles.find(); } }, { find: function() { return Groups.find({}, { sort: {name: 1} }); }, children: [{ find: function(group) { return Members.find({groupId: group._id}); }, children: [{ find: function(member) { return Meteor.users.find({_id: member.memberId}); } }] }] }]; }); ,您需要传递一系列将变为行的对象。

.bulkCreate