server: error on post with no files

This commit is contained in:
Max Leiter 2022-03-28 11:36:46 -07:00
parent de54754833
commit 73e2edfe2b
No known key found for this signature in database
GPG key ID: A3512F2F2F17EBDA

View file

@ -56,6 +56,10 @@ posts.post(
throw new Error("All files must have a title")
}
if (files.length === 0) {
throw new Error("You must submit at least one file")
}
const newPost = new Post({
title: req.body.title,
visibility: req.body.visibility,
@ -75,7 +79,7 @@ posts.post(
.update(file.content)
.digest("hex")
.toString(),
html
html: html || '',
})
await newFile.$set("user", req.body.userId)
@ -84,13 +88,12 @@ posts.post(
return newFile
})
)
await Promise.all(
newFiles.map((file) => {
newPost.$add("files", file.id)
newPost.save()
})
)
await newPost.save()
res.json(newPost)
} catch (e) {