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

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

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

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

Ruby on Rails 6

Ruby on Rails 6は、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Q&A

解決済

2回答

1117閲覧

【Vue.js】「 Error in render: "TypeError: Cannot read property 'title' of undefined" 」を解決したい

katahik

総合スコア79

Vue.js

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

Ruby on Rails 6

Ruby on Rails 6は、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

0グッド

0クリップ

投稿2021/08/02 00:56

概要

作成中のアプリ内で、詳細画面を作成しております。
そこで、詳細画面に遷移したときに、画面にはデータの中身は発生せず、consoleには「 Error in render: "TypeError: Cannot read property 'title' of undefined" 」このエラーが発生。
解決策について、ご教示いただけると幸いです。

![イメージ説明

コード

Show.vue

<template> <main> <section> <div> <table> <thead> <tr> <th>タイトル</th> <th>詳細</th> </tr> </thead> <tbody> <tr> <td>{{ latest_information.title }}</td> <td>{{ latest_information.detail }}</td> </tr> </tbody> </table> </div> </section> </main> </template> <script> export default { data() { return { latest_information: {}, } }, methods: { setData() { this.$loading.load(this.$auth.api.get(`admin/latest_informations/${this.$route.params.id}.json`) .then(response => { this.latest_information = response.data.latest_information }) .catch(err => { this.$errorHandlers.initial(err); })); }, }, mounted() { this.setData(); }, } </script> ```      #### コマンド「rails routes」の結果

api_v1_admin_latest_information GET /api/v1/admin/latest_informations/:id(.:format) api/v1/admin/latest_informations#show {:format=>:json}

#### latest_informations_controller.rb

module Api::V1::Admin
class LatestInformationsController < Api::V1::ApiController
before_action :set_latest_information, only: [:show, :destroy]

def show render json: @latest_information end private def set_latest_information @latest_information = LatestInformation.find(params[:id]) end

end
end

#### show.json.jbuilder

json.set! :latest_information do
json.id @latest_information.id
json.title @latest_information.title
json.detail @latest_information.detail
end

## 試したこと #### ・controller内にbyebugを仕込んで、@latest_informationの値を確認

(byebug) @latest_information
#<LatestInformation id: 3, title: "���������", detail: "���������", created_at: "2021-08-01 22:01:39", updated_at: "2021-08-01 22:01:39">

#### ・jbuilderで作成されたJSONデータを確認

irb(main):040:0> Jbuilder.encode do |json|
irb(main):041:1* json.set! :latest_information do
irb(main):042:2* json.id LatestInformation.find(3).id
irb(main):043:2> json.title LatestInformation.find(3).title
irb(main):044:2> json.detail LatestInformation.find(3).detail
irb(main):045:2> end
irb(main):046:1> end
LatestInformation Load (1.3ms) SELECT latest_informations.* FROM latest_informations WHERE latest_informations.id = 3 LIMIT 1
LatestInformation Load (1.0ms) SELECT latest_informations.* FROM latest_informations WHERE latest_informations.id = 3 LIMIT 1
LatestInformation Load (0.7ms) SELECT latest_informations.* FROM latest_informations WHERE latest_informations.id = 3 LIMIT 1
=> "{"latest_information":{"id":3,"title":"てst","detail":"てst"}}"

## 環境 rails 6系 vue@2.6.10

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

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

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

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

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

guest

回答2

0

自己解決

this.$loading.load(this.$auth.api.get(admin/latest_informations/${this.$route.params.id}.json)
.then(response => {
this.latest_information = response.data.latest_information
の中で、
console.log(response)を行ったところ、response.data.titleやdetailで値を受け取っていたため、
this.latest_information = response.data.title
this.latest_information = response.data.detailに修正した

投稿2021/08/02 09:52

katahik

総合スコア79

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

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

0

mountdライフサイクルフックで取得した値を変数latest_informationに返していない気がします。
mounted内でメソッドを使った場合は、その戻り値をmounted内で受け取り、data変数に返さないとDOMに反映されなかったはず。

投稿2021/08/02 01:06

編集2021/08/02 01:08
FKM

総合スコア3644

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.46%

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

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

質問する

関連した質問