##課題
messagesコレクションの各tag毎にtime値を割り当てようとしているのですが、中々うまくいきません。
現在のアプリは以下の様に掲示板にtag機能を実装し、また、tag毎にtime値を入力できる様にしてあります。
上記のhtml:10,css:20
やhtml:30,css:40
はそれぞれのtagと紐づけたいと思っているのですが、その処理がうまくいきません。
現状送信ボタンでイベントが発火し、以下の様になります。
![
上記のdataは2回目の送信ボタンを押した時点でのdbの中身です。
2回目は、html:30,css:40
と入力してあるので、本来ならばtagの中身はhtml:30,css:40
となっていなくてはならないです。
しかし、現状html:40,css:40
となっている始末です。
##試したこと
TextBox.vueのNumber(this.tagTimes)
がdialogで入力したそのtagのtime値になります。この値をmessagesコレクションの各tag毎に保存したいです。
しかし、TextBox.vue248行目でthis.select の値を変更している為苦戦しております。
(また、watch
プロパティで変更があった際に関数が実行されるので、そのやり方も良くないのかなと思いました。しかし、tag毎の時間を入力するには、tagを選択→変更があった為watchが動き(その際にwatchの中の関数も実行)→tag毎にtime値を保存の流れが適切かと思いました。)
→ここでthis.selectの値を変更している理由としては、tagsコレクションにはそれぞれ入力したtagのデータを格納したいと考えており、dialogで決定ボタンを押すと、どんどんtime値が加算されていく仕組みにしています。
TextBox.vue
watch: { select(val, prev) { if (val.length === prev.length) return; this.select = Array.prototype.map.call(Object(val), value => { if (typeof value === 'string') { value = { text: value }; this.items.push(value); } // ここでtag事の学習時間をdialogで入力させることができたらいいかも this.dialogTime(); return value; }); } }, methods: { async add() { this.canPost = false; try { const message = await MessageModel.save({ times: Number(this.times), bodys: this.bodys, // tags: this.select tags: this.dbMessagesSelect }); const uid = firebase.auth().currentUser.uid; this.select.forEach(async element => { const params = Object.assign(element, { uid: uid }); // console.log(params); const TagSame = await dbTags.where('text', '==', params.text).get(); if (TagSame.docs) { let Tag = []; TagSame.docs.forEach(e => { Tag = []; Tag.push(e.id); }); // console.log(await dbTags.doc(Tag[0]).get()); // console.log(element.time); // console.log(TagSame.docs); await dbTags.doc(Tag[0]).set({ text: params.text, time: params.time, uid: params.uid }); } }); this.onClick(message); this.times = 0; this.bodys = ''; this.select = ''; // this.dbMessagesSelect = ''; } catch (error) { alert(error.message); } this.canPost = true; }, dialogTime() { this.dialog = true; }, // dialog内の決定ボタンで発火 async tagTime() { this.dialog = false; this.dbMessagesSelect = this.select.map(e => { console.log(e); return e; }); // this.dbMessagesSelect = this.select; // this.dbMessagesSelect[this.dbMessagesSelect.length - 1].time = Number(this.tagTimes); console.log(this.dbMessagesSelect[this.dbMessagesSelect.length - 1].time); console.log(this.dbMessagesSelect); console.log(Number(this.tagTimes)); // 合計値を格納 this.times += Number(this.tagTimes); console.log(Number(this.tagTimes)); // tagとtimeを紐付け if (this.select[this.select.length - 1].time) { // dbMessages内のtagのtime値 // this.dbMessagesSelect[this.dbMessagesSelect.length - 1].time = Number(this.tagTimes); console.log(this.dbMessagesSelect); this.select[this.select.length - 1].time += Number(this.tagTimes); } else { Object.assign(this.select[this.select.length - 1], { time: Number(this.tagTimes) }); } this.tagTimes = 0; }, },
あなたの回答
tips
プレビュー