我有一個記錄列表,我正在用單個表單字段中的調整值進行批量更新。當我嘗試運行POST並根據輸入值更新記錄時,我在Where子句中遇到了一個錯誤,我想知道如何解析where子句的discoverySourceId
值或什麼是最好的方法來使用我當前的設置。Sequelize批量更新數組問題
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>
discoverySource:
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;
}
您錯過了更新調用中的where子句。將organizationId傳遞到where子句,否則它將更新DiscoverySource表中的每一行。 – grimurd
我應該如何包含where子句?我試過'models.DiscoverySource.update(sources,{where:{discoverySourceId:req.body.discoverySourceId}})',但收到'未處理的拒絕TypeError:build.set不是函數的錯誤 – cphill
我在這裏猜測,但req.body.discoverySourceId最有可能是一個字符串類型而不是數字類型(因爲隱藏的輸入被解析爲字符串,而不是數字)。嘗試parseInt(req.body.discoverySourceId) – grimurd