如何从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'
});
相关文章