【问题标题】:How to delete object from firebase database如何从firebase数据库中删除对象
【发布时间】:2021-12-10 01:35:46
【问题描述】:

我正试图弄清楚如何删除提交到 firebase 数据库的信息。 我正在尝试删除请求下的信息。 Example

这是我用来获取数据的操作:

export default {
async contactArtist(context, payload) {
    const newRequest = {
        userEmail: payload.email,
        message: payload.message
    };
    const response = await fetch(`https://find-artist-d3495-default-rtdb.firebaseio.com/requests/${payload.artistId}.json`, {
        method: 'POST',
        body: JSON.stringify(newRequest)
    });

    const responseData = await response.json();

    if (!response.ok) {
        const error = new Error(responseData.message || 'Failed to send request.');
        throw error;
    }

    newRequest.id = responseData.name;
    newRequest.artistId = payload.artistId;

    context.commit('addRequest', newRequest);
},
async fetchRequests(context) {
    const artistId = context.rootGetters.userId;
    const token = context.rootGetters.token;
    const response = await fetch(`https://find-artist-d3495-default-rtdb.firebaseio.com/requests/${artistId}.json?auth=` + token);
    const responseData = await response.json();

    if (!response.ok) {
        const error = new Error(responseData.message || 'Failed to fetch requests.');
        throw error;
    }

    const requests = [];

    for (const key in responseData) {
        const request = {
            id: key,
            artistId: artistId,
            userEmail: responseData[key].userEmail,
            message: responseData[key].message
        };
        requests.push(request);
    }
    context.commit('setRequests', requests);
},

};

我正在尝试设置一个按钮来删除选定的请求对象。

【问题讨论】:

    标签: javascript firebase vue.js firebase-realtime-database vuex


    【解决方案1】:

    您的代码正在发送一个POST 请求,该请求告诉 Firebase 生成一个唯一密钥。来自saving data的文档:

    POST:添加到我们的 Firebase 数据库中的数据列表。每次我们发送 POST 请求时,Firebase 客户端都会生成一个唯一的密钥,例如 fireblog/users/<unique-id>/<data>

    delete a node,将DELETE 动词/方法发送到该路径:

    const response = await fetch(`https://find-artist-d3495-default-rtdb.firebaseio.com/requests/${payload.artistId}.json`, {
        method: 'DELETE'
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-20
      • 2019-09-25
      • 1970-01-01
      相关资源
      最近更新 更多