我在Xamarin Android App上从Firebase数据库中删除项目时遇到问题。
我收到400(错误请求)错误
删除方法:
private async void RemovePost(string ActivityID)
{
var firebase = new FirebaseClient(FirebaseURL);
await firebase.Child("posts").Child(ActivityID).DeleteAsync();
var intent = new Intent(mContext, typeof(MyProfileActivity));
mContext.StartActivity(intent);
}
型号:
public class Post
{
public string ActivityID { get; set; }
public string OwnerID { get; set; }
public string ActivityTitle { get; set; }
public string ActivitySport { get; set; }
public DateTime ActivityDate { get; set; }
public DateTime ActivityTime { get; set; }
public string ActivityTown { get; set; }
public string ActivityAddress { get; set; }
public string ActivityPhone { get; set; }
public string ActivityEmail { get; set; }
public string ActivityDetails { get; set; }
}
请注意,我传递给RemovePost
方法的活动ID为“0”
答案 0 :(得分:2)
那么为什么在首先将记录保存到数据库时不将密钥设为ActivityId
?
var post = new Post {
ActivityId = 0,
//...other properties populated
}
var record = await firebase
.Child("posts")
.Child(post.ActivityId)
.PostAsync(post));
var posts = await firebase
.Child("posts")
.OnceAsync<Post>();
foreach (var item in posts) {
Console.WriteLine($"{item.Key} post has id {item.Object.ActivityId}");
}
await firebase.Child("posts").Child(post.ActivityID).DeleteAsync();
在查询记录时,您可以查询已知密钥
答案 1 :(得分:0)
此:
await firebase.Child("posts").Child(ActivityID)
将尝试将ActivityID与节点的根匹配,在您的情况下,该值为 KiG7ju8CsDfGW3eSBIn
尝试将firebase删除调用更改为以下内容:
await firebase.Child("posts").OrderBy("ActivityID").EqualTo(ActivityID).DeleteAsync();
<强> 更新 强>
试试这个你可以调试。设置一个断点并遍历调用以查看结果是什么。
//Get the record(s). Can be more than one if ActivityID value is repeated.
var posts = await firebase
.Child("posts")
.OrderBy("ActivityID")
.EqualTo("0")
.OnceAsync<Post>();
// Select only the first one
var firstPost = posts.FirstOrDefault();
// send to delete this
await firebase
.Child("posts")
.Child(firstPost?.Key)
.DeleteAsync();