Auto add properties
This commit is contained in:
parent
dcc78c725b
commit
e93ad5d7b3
1 changed files with 35 additions and 1 deletions
|
@ -31,7 +31,41 @@ class Vegetables {
|
|||
|
||||
vegetables.create(newItem)
|
||||
.then(item => {
|
||||
models.properties
|
||||
.findAndCountAll()
|
||||
.then(properties => {
|
||||
if (properties && properties.rows.length > 0) {
|
||||
let saved = 0
|
||||
const _callback = () => {
|
||||
if (properties.count === saved) {
|
||||
callback(null, item)
|
||||
}
|
||||
|
||||
properties.rows.forEach(propperty => {
|
||||
models.vegetableProp
|
||||
.create({
|
||||
vegetablesId: item.id,
|
||||
propertyId: propperty.id
|
||||
})
|
||||
.then(() => {
|
||||
saved += 1
|
||||
_callback()
|
||||
})
|
||||
.catch((e) => {
|
||||
console.log(e)
|
||||
saved += 1
|
||||
_callback()
|
||||
})
|
||||
})
|
||||
}
|
||||
} else {
|
||||
callback(null, item)
|
||||
}
|
||||
})
|
||||
.catch(e => {
|
||||
item.destroy()
|
||||
callback(e, 500)
|
||||
})
|
||||
})
|
||||
.catch(e => {
|
||||
callback(e, null)
|
||||
|
|
Loading…
Reference in a new issue