简体   繁体   English

Firebase 删除不存在的键时数据库不返回错误

[英]Firebase Database doesn't return error when removing a nonexistent key

In the Database I deleted an actual key -LoGeKWGQMJ2EZ5b_Scp which worked fine.Database中,我删除了一个实际的密钥-LoGeKWGQMJ2EZ5b_Scp ,它运行良好。 But then I tried to delete the same exact key again and it printed successfully deleted .但后来我尝试再次删除完全相同的密钥,它打印成功删除 I then added a completely fake key 12345 and it still printed successfully deleted然后我添加了一个完全伪造的密钥12345 ,它仍然打印成功删除

guard let userId = Auth.auth().currentUser?.uid else { return }
let fakeKey = "12345"
    
let postsRef = Database.database().reference().child("posts").child(userId).child(fakeKey)
postsRef.removeValue { (error, _) in
    if let error = error {
        print(error.localizedDescription)
        return
    }

    print("successfully deleted") // always prints
}

I then tried an atomic delete to see what would happen and it printed successfully deleted for 1 real key and 1 fake key.然后我尝试了一个原子删除,看看会发生什么,它打印成功删除了 1 个真实密钥和 1 个假密钥。 Even after the real key was deleted and I tried again it still printed successfully deleted for both a fake key and a nonexistent key:即使在删除真实密钥并再次尝试后,它仍然打印成功删除了假密钥和不存在的密钥:

guard let userId = Auth.auth().currentUser?.uid else { return }
let fakeKey = "12345"

let postsRef = "/posts/\(userId)/\(fakeKey)"

// this is a real key  
let realKey = "-LvXTCmXfpzJA9SUBX8U"  
let postsUserIdsRef = "/posts_userIds/\(userId)/\(realKey)"

var dict = [String: Any]()
dict.updateValue(NSNull(), forKey: postsRef)
dict.updateValue(NSNull(), forKey: postsUserIdsRef)

let rootRef = Database.database().reference()
rootRef.updateChildValues(dict, withCompletionBlock: { (error, _) in
        
    if let error = error {
        print(error.localizedDescription)
        return
    }
        
    print("successfully deleted") // always prints 
})

The odd thing is when I tried to do the same thing with storage I always get a does not exist error, which is what I expect奇怪的是,当我尝试对存储做同样的事情时,我总是得到一个does not exist的错误,这正是我所期望的

Object myUserId/-MHRkqMuc9c43MDNnbkw/-LoGeKWGQMJ2EZ5b_Scp.mp4 does not exist.

code:代码:

// for this example postId is a real key but the videoId is the nonexistent key from above
guard let userId = Auth.auth().currentUser?.uid else { return }
let realPostId = "-MHRkqMuc9c43MDNnbkw"
let nonexistentVideoId = "-LoGeKWGQMJ2EZ5b_Scp"

let storageRef = Storage.storage().reference().child(userId).child(realPostId).child("\(nonexistentVideoId).mp4")
storageRef.delete(completion: { (error) in

    if let error = error {
        print(error.localizedDescription) // prints Object myUserId/-MHRkqMuc9c43MDNnbkw/-LoGeKWGQMJ2EZ5b_Scp.mp4 does not exist.
        return
    }

    print("successfully deleted") // never prints
})

Btw, with Storage if you successfully delete something once and try to delete it again, it will return a does not exist error.顺便说Storage ,如果你成功删除了一些东西并再次尝试删除它,它会返回一个does not exist的错误。

Why is Database not returning an error for a nonexistent/previously deleted key and/or a fake key but Storage does return an error?为什么Database不为不存在/先前删除的密钥和/或假密钥返回错误,但Storage确实返回错误?

The Firebase Realtime Database considers a delete operation successful when the key doesn't exist after the operation completes. Firebase 实时数据库认为操作完成后键不存在时删除操作成功。 Whether the current operation actually deleted the key is not a factor in its success, so what you're seeing is the intended behavior.当前操作是否实际删除了密钥并不是其成功的因素,因此您看到的是预期的行为。

If you want to have an operation that only succeeds when the key was present initially and you then actively removed it, you will need to use a transaction .如果您希望只有当密钥最初存在然后您主动删除它时才会成功的操作,您将需要使用 transaction


Cloud Firestore follows the same logic for delete operations on non-existing documents, so it seems Cloud Storage is the odd one out here. Cloud Firestore 遵循相同的逻辑对不存在的文档进行删除操作,因此 Cloud Storage 似乎是这里的一个奇怪的地方。 There is no documented reason for this difference, as far as I know.据我所知,这种差异没有记录在案的原因。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Flutter Firebase Auth 上的编译错误:“必须返回非空值,因为返回类型‘从不’不允许 null。” - Flutter Compile Error on Firebase Auth: "A non-null value must be returned since the return type 'Never' doesn't allow null." FIREBASE 致命错误:无法确定 Firebase 数据库 URL - FIREBASE FATAL ERROR: Can't determine Firebase Database URL JAVA Android 工作室不发送数据到 Firebase 实时数据库 - JAVA Android studio Doesn't send data to Firebase Realtime Database 如果用户的 email 和密码不匹配,其他代码将不会运行并返回错误。 但如果这样做,那么它将在谷歌 firebase 中正常工作 - If user's email and password doesn't match the other codes won't run and return an error. But if does, then it will work fine in google firebase 未来 function 获取 Firebase 数据不会返回所需的 output - Future function fetching Firebase data doesn't return the desired output Firebase nodejs没有正确执行返回function - Firebase nodejs doesn't execute return function properly react-native-firebase 实时 database.ref 不起作用 - react-native-firebase realtime database .ref doesn't work 为什么我的 hashmap 没有被添加到 firebase 实时数据库中? - Why doesn't my hashmap get added to firebase realtime database? CK 编辑器不发送数据到 Firebase 数据库为空 - CK Editor doesn't send data to Firebase Database Empty 尝试从 Firebase 中读取 Firebase 数据库时无法确定 Firebase 数据库 URL Node.js Firebase function - Can't determine Firebase Database URL when trying to read Firebase Database from within Node.js Firebase function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM