gpt4 book ai didi

javascript - 为什么它读取[object Object]?

转载 作者:行者123 更新时间:2023-12-02 23:54:58 26 4
gpt4 key购买 nike

我正在尝试从 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"
}
}
}
}

最佳答案

您正在阅读的标题是:

var transcript_title = snap.child("titles").val();

翻译成这个 JSON:

  "titles" : {
"1" : "hi1",
"2" : "hi1",
"184" : "test2",
"230" : "test",
"1192012017" : "hi3",
"2019201220" : "test"
}

这是一个对象,因此当您将其设置到 HTML 中时,它将显示为 [object Object] (请注意,不同的浏览器对此的处理方式可能有所不同)。要在 HTML 中获取完整的 JSON 作为文本,您可以使用 JSON.stringify(...):

$("#read").append('<h4 id = "clicked">'+JSON.stringify(transcript_title)+'</h4>');

如果您想显示所有标题,可以循环显示它们:

snap.child("titles").forEach(function(titleSnap) {
$("#read").append('<h4 id = "clicked">'+titleSnap.val()+'</h4>');
})

关于javascript - 为什么它读取[object Object]?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55445018/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com