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
WARNING! Although there is a key with this ID in the database it does not verify this commit! This commit is SUSPICIOUS.
GPG key ID: A3512F2F2F17EBDA

View file

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