【发布时间】:2019-04-01 06:27:27
【问题描述】:
我正在尝试从 Firebase 中读取数据,但我不断收到此 [object Object]。我不知道是我读错了还是我的数据库有问题。我也尝试过使用 jQuery,但它不起作用。这可能是我想念的非常简单的事情。
//var transcript_title = null;
//var transcript = null;
//This is to intialize everything and auth. with the firebase server
var config = {
apiKey: "AIzaSyB9p1VvVfhnbrcDwUKUuSqw9aQsqnDi4nQ",
authDomain: "html5project-870df.firebaseapp.com",
databaseURL: "https://html5project-870df.firebaseio.com",
projectId: "html5project-870df",
storageBucket: "html5project-870df.appspot.com",
messagingSenderId: "54935462861"
};
//firebase.initializeApp(config);
//checks if it has been init
if (!firebase.apps.length) {
firebase.initializeApp({});
}
//declare variables
var database = firebase.database();
//tells where the items are going to be
var Rootref = database.ref().child("users");
var ref = firebase.database().ref("users");
ref.on("value", function(snapshot) {
console.log(snapshot.val());
}, function (error) {
console.log("Error: " + error.code);
});
//var Rootref1 = database.ref().child("users").child("id: 113295907411766134791")/*.child("trans")*/;
//used to retrieve data
Rootref.on("child_added",snap => {
//gets the child of titles stores it as variable
var transcript_title = snap.child("titles").val();
//var transcript_title = (snap.val() && snap.val().titles);
//gets the actual title and stores it as a var
//var transcript = snap.child(transcript_title).val();
$('#transcrip').val(snap.child('users/id: 107621796826103613669'))
//jquery - way to add html elemnts with javascript
$("#read").append('<h4 id = "clicked">'+transcript_title+'</h4>');
//$("#read").append('<h4 >Test</h4>');
//when button view was clicked it will show the transcriptiodn
});
{
"users" : {
"id: 107621796826103613669" : {
"hi1" : [ "test" ],
"hi2" : [ "hi" ],
"hi23" : [ "hi3" ],
"hi3" : [ "blaha" ],
"test" : [ "blahblah" ],
"test2" : [ "te2" ],
"titles" : {
"1" : "hi1",
"2" : "hi1",
"184" : "test2",
"230" : "test",
"1192012017" : "hi3",
"2019201220" : "test"
}
}
}
}
【问题讨论】:
-
您还没有说哪一行代码正在输出它,但这意味着您正在尝试显示一个变量,该变量包含一个预期字符串的对象,因此类型被强制。要解决此问题,您需要访问对象的相关属性并显示这些属性,而不是整个对象本身。
-
“IT”是一个小丑......并且“它不会读取。”它正在处理“它......很可能是一台 PC。重点是......语言解释器读取了一些东西...并且您指的是“读取错误”或其他错误类型。在您的问题标签中说明它。它现在确实命中了“分类审查”并将其发送到“需要编辑”,以便您可以修复和重新制定你的问题标签正确。还包括 Rory 的评论,因为它反映了你对这些信息做了一些事情。
标签: javascript jquery html firebase firebase-realtime-database