Node + Sequelize:如何在添加之前检查项目是否存在? (异步混淆) [英] Node + Sequelize: How to check if item exists before adding? (async confusion)
问题描述
我很遗憾对节点很新,并且对节点的异步/同步执行产生了一些困惑。
我正在使用节点,使用sqlite和async.js进行sequelize 。
我有一系列文章
,每个都有的作者
。
每个文章中的每个
,我想检查作者
作者
是否存在。如果没有,请创建它。
问题是,在初始运行时,正在创建重复的作者,我假设由于异步功能导致检查存在的问题。
例如,使用数组: authors = ['A。测试','B。测试','C。测试','A。测试']
和代码:
async.each(authors,function(item,callback){
Author.sync()。then(function(){
Author.count({where:{name:item.trim(然后(函数(计数){
if(count!= 0){
console.log('作者已存在')
} else {
console .log('Creating author ...')
Author.create({
name:item.trim()
})
}
})
})
})
首次运行时,将创建一个表:
ID | name
------------
0 | A.测试
1 | B.测试
2 | C.测试
3 | A.测试
我做错了什么?我似乎错过了Node中异步与同步执行的基本概念。
(我也尝试过async.eachSeries,它应该是串行执行而不是并行?)
编辑:略有重构,但仍然会产生重复
async.eachSeries(authors,function(authorName,callback){
Author.findOne({where:{name:authorName.trim()}})。
then( function(author){
if(author){
//作者存在...
callback()
} else {
//作者不存在。 ..
Author.create({
name:authorName.trim()
})。then(function(author){
callback()
})
}
})
})
Author.count
。请参阅 findOrCreate()。
使用 findOrCreate()
,您可以拥有以下内容。 (为此编辑了trex005的代码段)
async.eachSeries(authors,function( item,callback){Author.sync()。then(function(){Author.findOrCreate({where:{name:item.trim()},默认值:{//设置默认属性,如果它不存在名称:item.trim()}})。then(function(result){var author = result [0],//创建者的实例= result [1]; //布尔表明它是否已创建,如果(!created){//如果作者已存在且未创建,则为false.console.log('作者已存在');} console.log('Created author ...'); callback();});} )})
I am unfortunately new to node and running into some confusion regarding the asynchronous/synchronous execution of node.
I am using node, sequelize with sqlite and async.js.
I have a series of Articles
, each of which has a number of Authors
.
For each Authors
in each Article
, I'd like to check if the Author
exists. If not, create it.
The problem is, on the initial run, duplicate authors are being created, I assume due to asynchronous functionality causing an issue with checking for existence.
For example, with the array: authors = ['A. Test', 'B. Test', 'C. Test', 'A. Test']
and the code:
async.each(authors, function(item, callback){
Author.sync().then(function(){
Author.count({ where: {name: item.trim()} }).then(function(count){
if (count != 0) {
console.log('Author already exists')
} else {
console.log('Creating author...')
Author.create({
name: item.trim()
})
}
})
})
})
On the first run, will create a table:
ID | name
------------
0 | A. Test
1 | B. Test
2 | C. Test
3 | A. Test
What am I doing wrong? I seem to be missing a fundamental concept of asynchronous vs synchronous execution in Node.
(I've also tried async.eachSeries which is supposed to execute in series rather than in parallel?)
Edit: Slightly refactored, but still creating duplicates
async.eachSeries(authors, function(authorName, callback){
Author.findOne({ where: {name: authorName.trim()} }).
then(function(author){
if (author) {
// Author exists...
callback()
} else {
// Author does not exist...
Author.create({
name: authorName.trim()
}).then(function(author){
callback()
})
}
})
})
Author.count
isn't really needed, unless you need the count. See findOrCreate().
With findOrCreate()
you could have the following. (Edited trex005's snippet for this)
async.eachSeries(authors, function(item, callback) {
Author.sync().then(function() {
Author.findOrCreate({
where: {
name: item.trim()
},
defaults: { // set the default properties if it doesn't exist
name: item.trim()
}
}).then(function(result) {
var author = result[0], // the instance of the author
created = result[1]; // boolean stating if it was created or not
if (!created) { // false if author already exists and was not created.
console.log('Author already exists');
}
console.log('Created author...');
callback();
});
})
})
这篇关于Node + Sequelize:如何在添加之前检查项目是否存在? (异步混淆)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!