Mysql Sequelize TypeError build.save不是函数
我有一些jQuery逻辑设置,用户可以在其中提交多个字段值,这些字段值应该作为数据库中的单个记录创建。我以前的解决方案是将我的值映射到一个变量,然后与Mysql Sequelize TypeError build.save不是函数,mysql,express,sequelize.js,Mysql,Express,Sequelize.js,我有一些jQuery逻辑设置,用户可以在其中提交多个字段值,这些字段值应该作为数据库中的单个记录创建。我以前的解决方案是将我的值映射到一个变量,然后与.bulkCreate方法一起使用,但我不知道MYSQL不支持使用此方法自动递增字段。因此,我决定采用我的逻辑,而是使用.create方法创建for循环。很遗憾,我收到了以下错误消息:TypeError:this.build(…).save不是models.DiscoverySource.create(sources)行中的函数。当我不使用构建方法
.bulkCreate
方法一起使用,但我不知道MYSQL不支持使用此方法自动递增字段。因此,我决定采用我的逻辑,而是使用.create
方法创建for循环。很遗憾,我收到了以下错误消息:TypeError:this.build(…).save不是models.DiscoverySource.create(sources)
行中的函数。当我不使用构建方法时,为什么会出现此消息
.post(function(req, res){
console.log(req.body.discoverySource);
var sources = _.map(req.body.discoverySource, function (source) {
return {
discoverySource: source,
organizationId: req.body.organizationId
};
});
console.log("These are the" + sources); //outputs "These are the [object Object],[object Object], [object Object]
console.log(sources[0].discoverySource); //outputs correctly first value
console.log(sources[0].organizationId); //outputs correctly the first value
for (i=0; i < sources.length; i++){
models.DiscoverySource.create(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);
});
}
});
.post(功能(请求、恢复){
控制台日志(请求主体发现源);
var sources=\映射(req.body.discoverySource,函数(源){
返回{
发现来源:来源,
organizationId:req.body.organizationId
};
});
log(“这些是“+源);//输出”这些是[object object]、[object object]、[object object]
console.log(sources[0].discoverySource);//正确输出第一个值
console.log(sources[0].organizationId);//正确输出第一个值
对于(i=0;i
创建
方法无法接受对象数组。
如果您需要ID,并且需要等待所有对象创建完毕,则需要等待所有单个create
承诺:
var promises = sources.map(source => models.DiscoverySource.create(source));
Promise.all(promises)
.then(() => models.DiscoverySource.findAll())
.then(discoverySource => {
console.log(discoverySource);
res.redirect('/app');
}).catch(error => {
res.send(error);
console.log('Error during Post: ' + error);
});