如何从FireBase数据库中删除对象 [英] How to delete object from firebase database

查看:30
本文介绍了如何从FireBase数据库中删除对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试找出如何删除提交给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);
},

};

我正在尝试设置将删除所选请求对象的按钮。

推荐答案

您的代码正在发送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'
});

这篇关于如何从FireBase数据库中删除对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆