如何从firebase数据库中删除对象



我想弄清楚如何删除提交给firebase数据库的信息。我正在尝试删除请求下的信息。示例

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

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生成一个唯一密钥。关于保存数据的文档:

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

删除一个节点,将DELETE动词/方法发送到该路径:

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

最新更新