diff --git a/server/api/documents.js b/server/api/documents.js index e482c79d7..0ad1ee5b5 100644 --- a/server/api/documents.js +++ b/server/api/documents.js @@ -209,6 +209,7 @@ router.post('documents.update', auth(), async ctx => { const user = ctx.state.user; const document = await Document.findById(id); + const collection = document.collection; if (!document || document.teamId !== user.teamId) throw httpErrors.NotFound(); @@ -216,16 +217,18 @@ router.post('documents.update', auth(), async ctx => { if (title) document.title = title; if (text) document.text = text; document.lastModifiedById = user.id; - await document.save(); - const collection = await Collection.findById(document.atlasId); - if (collection.type === 'atlas') { - await collection.updateDocument(document); - document.collection = collection; - } + const [updatedDocument, updatedCollection] = await Promise.all([ + document.save(), + collection.type === 'atlas' + ? await collection.updateDocument(document) + : Promise.resolve(), + ]); + + updatedDocument.collection = updatedCollection; ctx.body = { - data: await presentDocument(ctx, document), + data: await presentDocument(ctx, updatedDocument), }; });