Section.find({_id: article.section}).exec()
.then(section => {
if (section.length === 0) throw new Error('Section not found')
let tagsPromises = []
for (let t of article.tags) {
tagsPromises.push(Tag.find({_id: t}))
}
Promise.all(tagsPromises)
.then(tags => {
tags.map(tag => {
if (tag.length === 0) throw new Error('Tag not found')
})
article.save()
.then(result => {
Section.findOneAndUpdate({ _id: article.section }, {$push: {"articles": article._id}}, {new: true})
.then(result => {
console.log(result)
let tagsPromises = []
for (let t of article.tags) {
tagsPromises.push(Tag.findOneAndUpdate({ _id: t }, {$push: {"articles": article._id}}, {new: true}))
}
Promise.all(tagsPromises)
.then(result => {
res.status(201).json({
status: 'OK',
article
})
})
})
})
.catch(err => next(err))
})
.catch(err => next(err))
})
.catch(err => next(err))
Section.find({_id: article.section}).exec()
.then(section => {
console.log(section)
if (section.length === 0) throw new Error('Section not found')
for (let t of article.tags) {
Tag.find({_id: t})
.then(tag => {
console.log(tag)
if (tag.length === 0) throw new Error('Tag not found')
})
.catch(err => next(err))
}
})
.then(result => {
console.log('HELLO')
article.save()
.then(result => {
// console.log(result)
res.status(201).json({
status: 'OK',
article
})
})
.catch(err => next(err))
})
.catch(err => next(err))