使用SequelizeJS查找或创build

我尝试使用SequelizeJS的“findOrCreate”函数,但它不起作用。

“创build”的variables是“未定义”,所以我不知道为什么,因为它是由SequelizeJS使用的variables…

for( var i = 0; i < tags.length; i++ ){ global.db.Tag.findOrCreate({name: tags[i]}).success( function(tag, created){ if( created ){ global.db.PostTag.create({ PostId: id, TagId: tag.id }); } }); } 

我有同样的问题。 从1.7.0-alpha2升级到2.0.0-alpha2解决了它。 干杯

 global.db.Tag.findOrCreate({ where:{ name: tags[i] }, defaults: { //properties you want on create } }).then( function(tag){ var created = tag[1]; tag = tag[0]; //new or found if( created ){ } }).fail(function(err) { }); 

对我来说,答案是改变我的承诺parsing器从。然后到一个.spread。 这与Sequelize返回数据的格式有关。

 for( var i = 0; i < tags.length; i++ ){ global.db.Tag.findOrCreate({ name: tags[i] }).spread( function(tag, created){ if( created ){ global.db.PostTag.create({ PostId: id, TagId: tag.id }); } }); }