gpt4 book ai didi

node.js - Sequelize 批量更新数组问题

转载 作者:行者123 更新时间:2023-12-03 22:23:55 25 4
gpt4 key购买 nike

我有一个记录列表,我希望使用各个表单字段中的调整值进行批量更新。当我尝试运行 POST 并根据输入中的值更新记录时,我在 Where 子句中遇到了错误,我想知道如何解析 where 子句的 discoverySourceId 值或什么是最好的用于我当前设置的方法。

Error: Missing where attribute in the options parameter passed to update.

路线:
var appRoutes   = express.Router();
var _ = require('lodash-node');
var async = require('async');
var models = require('../models/db-index');

appRoutes.route('app/settings/discovery-sources')

.get(function(req, res){
models.DiscoverySource.findAll({
where: {
organizationId: req.user.organizationId
}, attributes: ['discoverySourceId', 'discoverySourceName']
}).then(function(discoverySource){
res.render('pages/app/settings-discovery-sources.hbs', {
discoverySource: discoverySource
});
})
})
.post(function(req, res){
console.log('POST Triggered');
var sources = _.map(req.body.discoverySourceName, function (source) {
return {
discoverySourceName: source,
discoverySourceId: req.body.discoverySourceId
};
});
models.DiscoverySource.update(sources).then(function(){
console.log("Successful update");
res.redirect('/settings/discovery-sources');
});
});

**Form:**

<div class="row">
<div class="col-md-8 col-md-offset-2">
<div class="annotation-form">
<h2>Discovery Sources</h2>
<form action="/app/settings/discovery-sources" method="post">
{{#each discoverySource}}
<input type="hidden" name="discoverySourceId" value={{this.discoverySourceId}}>
<input type="text" name="discoverySourceName[0]" value="{{this.discoverySourceName}}"><a href="#" id="settings-delete-discovery-source">Delete</a>
<br />
{{else}}
<p>No Discovery Sources</p>
{{/each}}
<button type="submit">Update Sources</button>
</form>
</div>
</div>
</div>

发现来源:
module.exports = function(sequelize, DataTypes) {

var DiscoverySource = sequelize.define('discovery_source', {
discoverySourceId: {
type: DataTypes.INTEGER,
field: 'discovery_source_id',
autoIncrement: true,
primaryKey: true,
notNull: true,
},
discoverySourceName: {
type: DataTypes.STRING,
field: 'discovery_source_name'
},
organizationId: {
type: DataTypes.TEXT,
field: 'organization_id'
},
},{
freezeTableName: true
});
return DiscoverySource;
}

最佳答案

var appRoutes = express.Router();
var _ = require('lodash-node'); // just use lodash, using lodash-node is deprecated and it's redundant.
var async = require('async');
var models = require('../models/db-index');

// Added these for context
var app = express();
var Promise = require('bluebird'); // or Q if you prefer.

// Make sure you're using body-parser urlencoded
// with extended set to true. This allows us to have
// fancy body parsing for forms.
// This line must be above all your routes.
var bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({
extended: true
}))

appRoutes.route('app/settings/discovery-sources')
.get(function (req, res) {
models
.DiscoverySource
.findAll({
where: {
organizationId: req.user.organizationId
},
attributes: ['discoverySourceId', 'discoverySourceName']
})
.then(function (discoverySource) {
res.render('pages/app/settings-discovery-sources.hbs', {
discoverySource: discoverySource
});
})
})
.post(function (req, res) {
console.log('POST Triggered');
// We use promise.map to map the array in the body into an array of promises
// which are resolved when each promise has been resolved.
// It's a good idea here to use sequelize.transaction() so that if one update
// fails, everything gets rolled back.
// If you're using Q it doesn't support .map() so this looks slightly different in that case.
// q.all(_.map(req.body.discoverySources, function() {}))
Promise
.map(req.body.discoverySources, function (source) {
return models
.DiscoverySource
.update({
discoverySourceName: source.name
}, {
// We have to call update with the ID of each discoverySource so that
// we update the names of each discovery source correctly.
where: {
discoverySourceId: source.id
}
})
})
.then(function () {
console.log("Successful update");
res.redirect('/settings/discovery-sources');
});
});

HTML
<div class="row">
<div class="col-md-8 col-md-offset-2">
<div class="annotation-form">
<h2>Discovery Sources</h2>
<form action="/app/settings/discovery-sources" method="post">
{{#each discoverySource}}
<!-- Use body parser urlencoded syntax so the request body is parsed into an array of objects with the
properties id and name. See the qs library for syntax documentation, https://www.npmjs.com/package/qs#readme -->
<input type="hidden" name="discoverySources[][id]" value={{this.discoverySourceId}}>
<input type="text" name="discoverySources[][name]" value="{{this.discoverySourceName}}"><a href="#" id="settings-delete-discovery-source">Delete</a>
<br /> {{else}}
<p>No Discovery Sources</p>
{{/each}}
<button type="submit">Update Sources</button>
</form>
</div>
</div>
</div>

关于node.js - Sequelize 批量更新数组问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37232399/

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