質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.46%
Vue.js

Vue.jsは、Webアプリケーションのインターフェースを構築するためのオープンソースJavaScriptフレームワークです。

Nuxt.js

Nuxt.jsは、ユニバーサルなSPAが開発可能なVue.jsベースのフレームワーク。UIの描画サポートに特化しており、SSRにおけるサーバーサイドとクライアントサイドのUIレンダリングなどさまざまな機能を持ちます。

Q&A

0回答

913閲覧

[Vue]TypeError: Cannot read property 'ref' of undefinedが解決できません[Nuxt]

jime1234567

総合スコア3

Vue.js

Vue.jsは、Webアプリケーションのインターフェースを構築するためのオープンソースJavaScriptフレームワークです。

Nuxt.js

Nuxt.jsは、ユニバーサルなSPAが開発可能なVue.jsベースのフレームワーク。UIの描画サポートに特化しており、SSRにおけるサーバーサイドとクライアントサイドのUIレンダリングなどさまざまな機能を持ちます。

0グッド

0クリップ

投稿2020/12/24 05:39

Nuxt.js初学者で技術書やネット記事を参考にしながらアプリを作っています。

https://note.com/kawa1228/n/n9687ceb3152d

上記の記事を基にfirebaseを使い画像とテキストを投稿する機能を実装しようとしているのですが

TypeError: Cannot read property 'ref' of undefined

というエラーが発生し画像とテキストをデータベース(firebase)に保存できないという状況に陥っています。

該当のソースコード

**app/posts/pages/new.vue** <template> <div class="post-wrapper"> <div class="post-title"> <h2>投稿ページ</h2> </div> <div class="post-name-wrapper"> <input type="text" class="post-name" v-model="p_name" placeholder="選手の名前" /> </div> <div class="post-text-wrapper"> <textarea class="post-text" v-model="body" placeholder="コメント"/> </div> <div class="post-image-wrapper"> <img v-if="preview" src="preview"/> <input type="file" accept="image/png,image/jpeg" @change="previewImage"/> </div> <div class="post-btn-wrapper"> <button class="post-btn" @click="postContents">投稿</button> </div> </div> </template> <script> export default { data() { return { file: null, preview: '', fileName: '', body: '', p_name:'', } }, methods: { previewImage(e) { const file = e.target.files[0]; this.file = file const reader = new FileReader() reader.onload = (e) => { this.preview = e.target.result this.fileName = file.name } reader.readAsDataURL(file) }, async postContents() { const d = new Date() const today = d.toLocaleDateString() const contents = { note: { created_at: today, title: this.title, p_name:this.p_name, body: this.body, }, image: { file: this.file, name: this.fileName } } await this.$store.dispatch('postContents', contents) console.log('load finish') // dataをclearにする } } } </script> <style> .post-title h2 { font-size: 28px; } .post-name-wrapper { margin-top: 20px; } .post-name { width: 300px; background-color: #ffff; border: 1px solid ; height: 25px; } .post-image-wrapper { display: flex; flex-direction: column; } .post-text-wrapper { display: flex; flex-direction: column; margin: 10px 0 10px 0; } .post-text { width: 500px; height: 200px; background-color: #ffff; border: 1px solid ; } .post-image-wrapper { margin: 10px 0; width: 300px; } .post-btn-wrapper { display: inline-block; } .post-btn { padding: 10px 50px; background-color: #ffff; border: solid 1px; } </style>
**app/store/index.js** import { auth ,storage, db } from '~/plugins/firebase' export const strict = false export const state = () => ({ user: null, }) export const mutations = { setUser(state, payload) { state.user = payload } } export const actions = { signUp({ commit }, { email, password }) { return auth().createUserWithEmailAndPassword(email, password) }, signInWithEmail({ commit }, { email, password }) { return auth().signInWithEmailAndPassword(email, password) }, signInWithGoogle({ commit }){ return auth().signInWithPopup(new auth.GoogleAuthProvider()) }, signOut() { return auth().signOut() }, // 投稿機能(仮)始まり async postContent(context, payload) { const contents = payload const loadImage = await context.dispatch('uploadImage', { name: contents.image.name, file: contents.image.file }) contents.image = loadImage const articlesRef = db.collection('articles') await articlesRef.add(contents) }, uploadImage(context, payload) { if (!payload.file) { return { name: 'サンプル画像', src: 'https://placehold.jp/150x150.png' } } const storageRef = storage.ref() return new Promise((resolve, reject) => { storageRef .child(`images/${payload.name}`) .put(payload.file) .then((snapshot) => { snapshot.ref.getDownloadURL().then((url) => { resolve({ name: payload.name, src: url }) }) }) .catch((err) => { console.log('画像投稿エラー', err) }) }) } // 投稿機能(仮)終わり } export const getters = { user(state){ return state.user }, isAuthenticated (state) { return !!state.user } }

試したこと

このエラーについて調べてみたところ同様のエラーが出ている状況でv-ifをv-showに置き換えたら解決できたという記事をいくつか見つけて試してみたのですが解決できませんでした。

この問題について何かお分かりになる方がいらっしゃいましたらアドバイスお願いします。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.46%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問