簡體   English   中英

Firebase 函數實時數據庫將數組轉換為對象問題

[英]Firebase Functions Realtime Database converting array to objects problem

當我嘗試將新數據添加到 firebase 實時數據庫時,它會將我的數組轉換為對象。 例子:

"POINTS": [{
  "w":237.00004067230424,
  "x":0,
  "y":0,
  "h":8.996961455599944
},
{
  "w":68.00003035736216,
  "h":243.99697579886254,
  "x":237,
  "y":9
}]

在實時數據庫中:

"POINTS":{
  "0":{
    "h":0.9969740288429421,
    "w":96.00002364349457,
    "x":0,
    "y":0
  },
  "1":{
    "h":163.99698397757402,
    "w":41.000020286560755,
    "x":97,
    "y":1
  }
}

我怎么解決這個問題? 我想像我試圖插入一樣得到數組

我的代碼:

在 firebase 函數中:我的數組是請求體的一部分

exports.createDB = functions.https.onRequest((request, response) => {
  response.header("Access-Control-Allow-Origin", "*");
  const myRequest = request.body;
  
  functions.logger.info(String(request.body));
  database.ref("diagram/"+myRequest.user.uid).set(myRequest);
  response.send(request.body);
});

我正在使用 postman 發送數據

已解決的問題:當我使用此 function 獲取數據時

database.ref("diagram/"+request.body.uid).on("value", function(snapshot) {
    //response.send(snapshot);
    response.json(snapshot);
  });

但改變它解決了我的問題

database.ref("diagram/"+request.body.uid).get().then(function(snapshot) {
    response.json(snapshot.val());
  });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM