gpt4 book ai didi

Javascript : Save JSON data to firebase database

转载 作者:行者123 更新时间:2023-11-30 11:23:19 30 4
gpt4 key购买 nike

我有一个像字符串一样的 JSON 对象,它由函数 tableToJson() 返回。这是它的样子。

{
{"Item Code":"sthing","Product Name":"sthing","Qantity":"1","Unit Price":"0","Item Total":"0"},
{"Item Code":"sthing","Product Name":"sthing","Qantity":"1","Unit Price":"0","Item Total":"0"},
{"Item Code":"sthing","Product Name":"sthing","Qantity":"1","Unit Price":"0","Item Total":"0"},
{"Item Code":"sthing","Product Name":"sthing","Qantity":"1","Unit Price":"0","Item Total":"0"}
}

如何将其保存在 firebase 数据库中。我已经在上面用 JS 保存了一些数据,如下所示:

var databaseRef = firebase.database().ref('Bills');
function submitData(e){
e.preventDefault();
//variables for getting all values
var receiptDate = document.getElementById('date').innerHTML;
var receiptTime = document.getElementById('time').innerHTML;
var receiptBillNo = getInputValues('billNo');
var receiptCompName = getInputValues('companyName');
var receiptEmail = getInputValues('email');
var receiptPhone = getInputValues('phone');
var discount = getInputValues('Discount');
var total = getInputValues('sumTotal');

var newDataref = databaseRef.push();
var itemsJson = tableToJson();//THIS WILL RETURN THE ABOVE JSON STRING
//itemsJson = tableToJson();
//JSON.parse(itemsJson);

newDataref.set({

date: receiptDate,
time: receiptTime,
billNo: receiptBillNo,
company: receiptCompName,
email: receiptEmail,
phone: receiptPhone,
discount: discount,
total:total,
zitems: itemsJson

});
}

why it is saving as string.

看看我的 tableToJson()

function tableToJson(){
var tableId = document.getElementById('dataEntryTable');
var headName;
var headers = [];
var dataArray = [];
var rowCount = tableId.rows.length;
var colCount = tableId.rows[0].cells.length;

dataArray.push("[");
for(var i = 1; i < colCount; i++){
headName = tableId.rows[0].cells[i].innerHTML;
headers.push(headName);
}
console.log(headers);
for(var i = 1; i < rowCount; i++){
dataArray.push("\n{");
//FOR FIRST APPROACH:dataArray.push("\n/"" + i + "/" :{");
for(var j = 1; j < colCount; j++){
var currValue = tableId.rows[i].cells[j].childNodes[0].value;
dataArray.push("\"" + headers[j-1] + "\":" + "\"" + currValue +
"\"");
if(j < (colCount - 1)){
dataArray.push(",");
}
}
if( i < (rowCount - 1)){
dataArray.push("},");
}
else{
dataArray.push("}/n");
}
}
dataArray.push("]");
return dataArray.join("");
}

这段代码有问题吗?即使我将它解析为 JSON,它也会作为字符串发送。

最佳答案

有两个问题:

  1. 你的 tableToJson 函数返回一个字符串,你可以通过让它返回一个实际的 JSON 对象,或者调用 JSON.parse 来解决这个问题,就像 Jonas 回答的那样.

  2. 不幸的是,JSON.parse() 目前将失败,因为您的字符串不是有效的 JSON。您不能像您所做的那样嵌套对象。你要么需要给每个嵌套对象一个标签:

    {
    "one": {"Item Code":"sthing","Product Name":"sthing","Qantity":"1","Unit Price":"0","Item Total":"0"},
    "two": {"Item Code":"sthing","Product Name":"sthing","Qantity":"1","Unit Price":"0","Item Total":"0"},
    "three": {"Item Code":"sthing","Product Name":"sthing","Qantity":"1","Unit Price":"0","Item Total":"0"},
    "four": {"Item Code":"sthing","Product Name":"sthing","Qantity":"1","Unit Price":"0","Item Total":"0"}
    }

    或者返回一个数组而不是一个对象:

    [
    {"Item Code":"sthing","Product Name":"sthing","Qantity":"1","Unit Price":"0","Item Total":"0"},
    {"Item Code":"sthing","Product Name":"sthing","Qantity":"1","Unit Price":"0","Item Total":"0"},
    {"Item Code":"sthing","Product Name":"sthing","Qantity":"1","Unit Price":"0","Item Total":"0"},
    {"Item Code":"sthing","Product Name":"sthing","Qantity":"1","Unit Price":"0","Item Total":"0"}
    ]

如果您采用后一种方法,请花点时间阅读 Firebase's blog post on using arrays .

关于Javascript : Save JSON data to firebase database,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48973622/

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