\r\n```","answerCount":1,"upvoteCount":0,"datePublished":"2018-12-23T08:25:26.195Z","dateModified":"2022-01-12T10:55:45.698Z","suggestedAnswer":[{"@type":"Answer","text":"これで取得出来ます。\r\nfirebaseから取得するメソッドを作って、created()で呼び出せば大丈夫です。\r\n\r\nあと質問はそのままにしない方が良いかと思います。\r\n[過去の質問](https://teratail.com/questions/164735)\r\n同じ様な質問ですよね。\r\n\r\n```vue\r\n\r\n \r\n
test page
\r\n
key:{{ key }} ⇒ value:{{ data }}
\r\n
\r\n\r\n\r\n\r\n\r\n```\r\n\r\n```js\r\n# fireinit.js\r\n\r\nimport * as firebase from \"firebase/app\"\r\nimport \"firebase/database\"\r\nimport \"firebase/storage\"\r\n\r\nconst config = {\r\n apiKey: ,\r\n authDomain: ,\r\n databaseURL: ,\r\n projectId: ,\r\n storageBucket: ,\r\n messagingSenderId: \r\n}\r\n\r\n!firebase.apps.length ? firebase.initializeApp(config) : \"\"\r\nexport const database = firebase.database()\r\nexport const storage = firebase.storage()\r\nexport default firebase\r\n```\r\n\r\n```json\r\n# firebase\r\n\r\n{\r\n \"note-test\" : {\r\n \"abcdefgh\" : {\r\n \"100\" : \"あ\",\r\n \"101\" : \"い\",\r\n \"103\" : \"う\"\r\n }\r\n }\r\n}\r\n```","dateModified":"2018-12-24T16:34:21.835Z","datePublished":"2018-12-24T16:31:30.656Z","upvoteCount":0,"url":"https://teratail.com/questions/165520#reply-247253","comment":[{"@type":"Comment","text":"すみません、説明が下手でやりたいことと微妙に違います。。。\r\n\r\nkey:{{ key }} ⇒ value:{{ data }}
\r\n\r\nで展開するのはできているのですがnotesのcontentのみを配列で引っ張ってこれず。。。あとjavascript内で展開したいのですがそれがどうしてもできず。。。","datePublished":"2018-12-25T13:23:53.596Z","dateModified":"2018-12-25T13:23:53.596Z"},{"@type":"Comment","text":"いえ、中身を展開したのは分かりやすくする為です。\r\n\r\nあとはただのオブジェクトなので、contentを取得したいならばオブジェクト操作をしてあげるだけかと思うのですが・・\r\n何がしたいのかが分かりません。\r\n\r\n引用---------------------------------------------------------------------------------------------------\r\ndatabase.ref(`notes/${this.user.uid}/${newNoteKey}`).set({ content: value, date: today })\r\n--------------------------------------------------------------------------------------------------------\r\nご自身でidとkeyを入れてデータベースに格納してる訳ですから、そのキーでオブジェクト取れますよね?\r\n\r\nnotesから引っ張ってきたのなら、\r\nthis.notes[this.user.uid][newNoteKey]\r\nで\r\n{\r\n content:〇〇,\r\n date:〇〇\r\n}\r\nが取得出来ます。","datePublished":"2018-12-26T02:48:41.490Z","dateModified":"2018-12-26T02:49:24.345Z"},{"@type":"Comment","text":"提示してもらった方法で何故か引っ張れないのでなんでだろうという質問でした。。。","datePublished":"2018-12-29T06:05:55.827Z","dateModified":"2018-12-29T06:05:55.827Z"}]}],"breadcrumb":{"@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"item":{"@id":"https://teratail.com","name":"トップ"}},{"@type":"ListItem","position":2,"item":{"@id":"https://teratail.com/tags/Firebase","name":"Firebaseに関する質問"}},{"@type":"ListItem","position":3,"item":{"@id":"https://teratail.com/questions/165520","name":"firebaseのrealtimedatabaseの値が何故か取得できない"}}]}}}firebaseのrealtimedatabaseを下記のように保存しています。

これのcontentを配列で引っ張ってきたいんですがなぜかうまくいきません。。。
nuxtで下記のコードで書いているのですがcreatedで引っ張るところがわかっていません。
<template>
</template>
<script>
import firebase from '@/plugins/firebase'
import { mapState } from 'vuex'
export default {
name: 'mypage',
props: ['user'],
data (context) {
// コンポーネントをロードする前に毎回呼び出されます
return { note_content: 'hello', notes:[]}
},
// DOMが作成されたときに値を代入する
created:
// ここがうまく書けない。
computed: {
...mapState({
mypageUrl: (state) => `/user/${state.uid}`
})
},
methods: {
saveContent: function(value) {
// 新しいテキストのためのキーを取得
var newNoteKey = firebase.database().ref().child('notes').push().key;
var today = Date.now();
firebase
.database()
.ref('notes/' + this.user.uid + '/' + newNoteKey)
.set({content:value,date:today});
}
}
/*以下省略*/
}
</script>
これで取得出来ます。
firebaseから取得するメソッドを作って、created()で呼び出せば大丈夫です。
あと質問はそのままにしない方が良いかと思います。
過去の質問
同じ様な質問ですよね。
vue
1<template>
2 <div>
3 <h1>test page</h1>
4 <div v-for="(data, key) in notes" :key="key">key:{{ key }} ⇒ value:{{ data }}</div>
5 </div>
6</template>
7
8<script>
9import { database } from "@/services/fireinit.js"
10// import { mapState } from "vuex"
11
12export default {
13 props: ["user"],
14 data() {
15 return {
16 note_content: "hello",
17 notes: []
18 }
19 },
20 created() {
21 this.fetchData()
22 },
23 computed: {
24 ...mapState({
25 // mypageUrl: state => `/user/${state.uid}`
26 })
27 },
28 methods: {
29 saveContent(value) {
30 // const newNoteKey = database
31 // .ref()
32 // .child("note-test")
33 // .push().key
34 // const today = Date.now()
35 // database.ref(`notes/${this.user.uid}/${newNoteKey}`).set({ content: value, date: today })
36 },
37 async fetchData() {
38 await database.ref("note-test/abcdefgh").once("value", snapshop => {
39 this.notes = snapshop.val() || []
40 })
41 }
42 }
43}
44</script>
45
js
1# fireinit.js
2
3import * as firebase from "firebase/app"
4import "firebase/database"
5import "firebase/storage"
6
7const config = {
8 apiKey: <your key>,
9 authDomain: <your key>,
10 databaseURL: <your key>,
11 projectId: <your key>,
12 storageBucket: <your key>,
13 messagingSenderId: <your key>
14}
15
16!firebase.apps.length ? firebase.initializeApp(config) : ""
17export const database = firebase.database()
18export const storage = firebase.storage()
19export default firebase
json
1# firebase
2
3{
4 "note-test" : {
5 "abcdefgh" : {
6 "100" : "あ",
7 "101" : "い",
8 "103" : "う"
9 }
10 }
11}
15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.29%

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

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