Skip to content

Commit

Permalink
メッセージをfirestoreに保存しよう
Browse files Browse the repository at this point in the history
  • Loading branch information
s-yoshiii committed Jul 27, 2022
1 parent 0669771 commit 99cc9dc
Showing 1 changed file with 19 additions and 6 deletions.
25 changes: 19 additions & 6 deletions src/views/ChatBoard.vue
Expand Up @@ -57,7 +57,6 @@ export default {
components: {
SideBar,
},
async created() {
this.roomId = this.$route.query.room_id;
const roomRef = firebase.firestore().collection("rooms").doc(this.roomId);
Expand All @@ -66,7 +65,6 @@ export default {
await this.$router.push("/");
}
this.room = roomDoc.data();
console.log("room", this.room);
const snapshot = await roomRef
.collection("messages")
.orderBy("createdAt", "asc")
Expand All @@ -77,7 +75,6 @@ export default {
},
mounted() {
this.auth = JSON.parse(sessionStorage.getItem("user"));
console.log("auth", this.auth);
},
data: () => ({
messages: [],
Expand All @@ -93,7 +90,6 @@ export default {
["mdi-alert-octagon", "Spam"],
],
auth: null,
// invalid: false,
}),
computed: {
invalid() {
Expand All @@ -108,13 +104,30 @@ export default {
this.body = "";
},
submit() {
this.messages.unshift({
this.messages.push({
message: this.body,
name: this.auth.diplayName,
photoURL: this.auth.photoURL,
createdAt: firebase.firestore.Timestamp.now(),
});
this.body = "";
const roomRef = firebase.firestore().collection("rooms").doc(this.roomId);
console.log(roomRef.collection("messages"));
roomRef
.collection("messages")
.add({
message: this.body,
name: this.auth.diplayName,
photoURL: this.auth.photoURL,
createdAt: firebase.firestore.Timestamp.now(),
})
.then((result) => {
console.log("success", result);
this.body = "";
})
.catch((error) => {
console.log("fail", error);
alert("メッセージの送信に失敗しました。");
});
},
},
};
Expand Down

0 comments on commit 99cc9dc

Please sign in to comment.