放入数据库 Firebase RD、javascript 时如何增加或减少值

问题描述 投票:0回答:1

我在 Firebase 中有一个包含当前库存的数据库。我希望能够使用新值加上 input2 或减去 input1 放入 DOM 中的新值来更改它。如何用加上或减去新值来更新库存?

cart.js //从localstorage中的DOM中输入并选择数据

const select2 = localStorage.getItem('select2')
const select = localStorage.getItem('select')
const input2 = localStorage.getItem('input2')
const input1 = localStorage.getItem('input')

//按下更改数据库中的库存时将其放入 firebase 数据库 函数结账(){

              async function updateSelectValue() {
                try {
                  const baseURL = 'https://paronab-79d88-default-rtdb.europe-west1.firebasedatabase.app/'
                  const url = baseURL + `product/${select2}/${select}.json`
                  const response = await fetch(url)
                  let data = await response.json()
                 data + input2 
                 data - input1
                  console.log(totalInput)
                  // Send the updated data back to Firebase
                  const updateResponse = await fetch(url, {
                    method: 'PUT', // Use PUT to update the entire data
                    body: data,
                    headers: {
                      'Content-type': 'application/json; charset=UTF-8',
                    },
                  });
              
                  if (updateResponse.ok) {
                    console.log('Data updated successfully in Firebase!');
                  } else {
                    console.error('Failed to update data in Firebase.');
                  }
                } catch (error) {
                  console.error('Error while updating data:', error);
                }
              }
              
              // Usage example: Call the updateSelectValue function to update 'select' based on 'select2'
              updateSelectValue('select2', 'select');
            emp

tyCart() }

javascript firebase-realtime-database put
1个回答
0
投票

您可以使用原子服务器端增量操作。对于 REST API,您可以在此处找到语法。

© www.soinside.com 2019 - 2024. All rights reserved.