From e03c69ab8febc6093d092221724b3190a2da351d Mon Sep 17 00:00:00 2001 From: nztqa Date: Wed, 28 Jun 2017 15:19:53 +0900 Subject: Fix: id generation --- models/checklists.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/models/checklists.js b/models/checklists.js index 537aecb0..0ee62fa2 100644 --- a/models/checklists.js +++ b/models/checklists.js @@ -86,7 +86,13 @@ Checklists.mutations({ //for items in checklist addItem(title) { const itemCount = this.itemCount(); - const _id = `${this._id}${itemCount}`; + let idx = 0; + if (itemCount > 0) { + const lastId = this.items[itemCount - 1]._id; + const lastIdSuffix = lastId.substr(this._id.length); + idx = parseInt(lastIdSuffix, 10) + 1; + } + const _id = `${this._id}${idx}`; return { $addToSet: { items: { _id, title, isFinished: false } } }; }, removeItem(itemId) { -- cgit v1.2.3-1-g7c22