本文介绍了Firebase queryEqual不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我在Firebase中拥有以下层级结构: func requestedEvents(){ DataService.ds.REF_EVENTS.queryOrdered(byChild:eventParticipant ).observe(.childAdded,with:{(snap)in print(KEEEY:\(snap.key)) let value = snap.value as?NSDictionary 如果snap.exists(),value?[eventParticipant]!= nil { print(eventParticipant:\(value?[eventParticipant])) var valueArray = value? ()valueArray.enumerated(){ print(valueArray \(index):\(element)中的(index,element) ) //self.selectedContacts.append(element as!Participant) if element [partPhone] as!String ==00 11 111 1111{ print (KEY:\(snap.key)PHONE:\(element [partPhone])) self.eventKey = snap.key print(snap.key:\( snap.key)) //child (\"eventItem\").child(\"eventOrganized\").queryOrdered(byChild:snap.key).queryEqual(toValue:1) $ b $在 print(有快照键(Snapshot Key))中的DataService.ds.REF_USERS.child(eventItem)。child(eventOrganized)。child(snap.key).observe(.value, :\(snapshot))}) //DataService.ds.REF_USERS.queryOrdered(byChild:provider)。queryEqual(toValue:email)。observe(.value,with :{(snapshot)in // print(With Provider:\(snapshot.value)) //}) DataService.ds.REF_EVENTS.child (self.eventKey).observe(.value,其中:{(snap)in print(snap)如果snap.value是NSNull { print(Event is not found!)} else { let value = snap.value为? NSDictionary print(value?[eventLocation], value?[eventCurrency], value?[eventDesc], value?[eventBeginDate] , value?[eventEndDate], value?[eventCurrency], value?[eventLocation], value?[eventName], value?[eventDesc], value?[eventPrice]) if let eventAddress = value?[eventLocation],让eventCurrency = value?[eventCurrency],让eventDesc = value?[eventDesc],让eventBeginDate = value?[eventBeginDate], let eventEndDate = value?[eventEndDate], let eventImg = value?[eventCurrency], let eventLocation = value? [eventLocation], let eventName2 = value?[eventName], let eventNote = value?[eventDesc], let eventPrice = value?[eventPrice] { let eventDetails = Event( eventAddress:value?[eventLocation] as! String, eventCurrency:value?[eventCurrency] as! String, eventDesc:value?[eventDesc] as!字符串, eventBeginDate:value?[eventBeginDate] as! String, eventEndDate:value?[eventEndDate] as! String, eventImg:value?[eventCurrency] as!字符串, eventLikes:1, eventLocation:value?[eventLocation] as!字符串, eventName:value?[eventName] as!字符串,事件注意:value?[eventDesc] as! String, eventPrice:value?[eventPrice] as!字符串, eventCreated:value?[eventCreated] as!字符串, eventStatus:0 as!诠释? ) // print(eventDetails) let eventName = value?[eventName] as! String self.userPastEventNameList.append(eventName) self.events.append(eventDetails) print(eventAddress,eventCurrency,eventDesc,eventBeginDate,eventEndDate,eventImg,eventLocation,eventName2,eventNote,eventPrice )} 其他{ print(NULL FOUND) $ b DispatchQueue .main.async { self.tableView.reloadData()} self.removeLoadingScreen()}) //}) $ b} })} (snap.key:(snap.key)): snap。键:-KePXAuTADuTLTsZ7qbe snap.key:-KePjqMKrARQmp-gTss- snap.key:-KePsCS5rB4lTwy6GWea snap.key:-KePwdRQPFFX30_GDAOK snap.key:-KePyNVghX4MkSxI_1fx snap.key:-KePz200QsJp6CSb3bVN 如果我使用这个,那么我得到一个快照: DataService.ds.REF_USERS.queryOrdered (byChild:provider)。queryEqual(toValue:email)。在$ b $ print(With Provider:\(snapshot.value))})中察看(.value, 结果: 提供者:可选({ JIbrGLdfCDUpMUOHEu7KzRye8ZK2 = { provider = email; }; $ b如果我使用这个,那么我没有得到任何结果: $ p $ print(snap.key:\(snap.key)) DataService.ds.REF_USERS (快照键:\\)中的{(快照)).queryEqual(toValue:1).observe(.value, \\(snapshot.value))}) 结果: 带有快照键:可选(< null>) 有人可以帮我吗? 解决方案 pre > DataService.ds.REF_USERS.child(eventItem)。child(eventOrganized)。queryOrdered(byChild:snap.key).queryEqual(toValue:1).observe(.value,with: {(snapshot)in(With Snapshot Key:\(snapshot.value))}) 这是行不通的,因为你缺少对V9T3cEgEGPRmlkMQb32hxa5gG7L2节点的引用。 如果你想让所有的eventOrganized节点的值为1,那么你需要使用以下查询。 DataService.ds.REF_USERS.child(V9T3cEgEGPRmIkMQb32hxa5gG7L2)。child(eventItem) (Snapshot Value:\(snapshot.value)).child(eventOrganized)。queryOrderedByValue()。queryEqual(toValue:1).observe(.value,with:{(snapshot)in print )) 显然你不想直接引用V9T3cEgEGPRmlkMQb32hxa5gG7L2,是可重用性的一个变量的关键(也许你有,我不能告诉你的代码)。我想这就是你想要的。 I have the following hierarchy in my Firebase:func invitedEvents() { DataService.ds.REF_EVENTS.queryOrdered(byChild: "eventParticipant").observe(.childAdded, with: { (snap) in print("KEEEY:\(snap.key)") let value = snap.value as? NSDictionary if snap.exists() , value?["eventParticipant"] != nil{ print("eventParticipant:\(value?["eventParticipant"])") var valueArray = value?["eventParticipant"] as! [[String:AnyObject]] for (index, element) in valueArray.enumerated() { print("valueArray\(index): \(element)") //self.selectedContacts.append(element as! Participant) if element["partPhone"] as! String == "00 11 111 1111" { print("KEY:\(snap.key) PHONE:\(element["partPhone"])") self.eventKey = snap.key print("snap.key:\(snap.key)") //child("eventItem").child("eventOrganized").queryOrdered(byChild: snap.key).queryEqual(toValue: "1") DataService.ds.REF_USERS.child("eventItem").child("eventOrganized").child(snap.key).observe(.value, with: { (snapshot) in print("With Snapshot Key: \(snapshot)") }) //DataService.ds.REF_USERS.queryOrdered(byChild: "provider").queryEqual(toValue: "email").observe(.value, with: { (snapshot) in // print("With Provider:\(snapshot.value)") //}) DataService.ds.REF_EVENTS.child(self.eventKey).observe(.value, with: { (snap) in print(snap) if snap.value is NSNull { print("Event is not found!") } else { let value = snap.value as? NSDictionary print(value?["eventLocation"] , value?["eventCurrency"], value?["eventDesc"], value?["eventBeginDate"], value?["eventEndDate"], value?["eventCurrency"], value?["eventLocation"], value?["eventName"], value?["eventDesc"], value?["eventPrice"]) if let eventAddress = value?["eventLocation"] , let eventCurrency = value?["eventCurrency"], let eventDesc = value?["eventDesc"], let eventBeginDate = value?["eventBeginDate"], let eventEndDate = value?["eventEndDate"], let eventImg = value?["eventCurrency"], let eventLocation = value?["eventLocation"], let eventName2 = value?["eventName"], let eventNote = value?["eventDesc"], let eventPrice = value?["eventPrice"] { let eventDetails = Event( eventAddress: value?["eventLocation"] as! String, eventCurrency: value?["eventCurrency"] as! String, eventDesc: value?["eventDesc"] as! String, eventBeginDate: value?["eventBeginDate"] as! String, eventEndDate: value?["eventEndDate"] as! String, eventImg: value?["eventCurrency"] as! String, eventLikes: 1, eventLocation: value?["eventLocation"] as! String, eventName: value?["eventName"] as! String, eventNote: value?["eventDesc"] as! String, eventPrice: value?["eventPrice"] as! String, eventCreated: value?["eventCreated"] as! String, eventStatus: 0 as! Int? ) //print(eventDetails) let eventName = value?["eventName"] as! String self.userPastEventNameList.append(eventName) self.events.append(eventDetails) print(eventAddress,eventCurrency,eventDesc, eventBeginDate, eventEndDate, eventImg, eventLocation, eventName2, eventNote, eventPrice) } else { print("NULL FOUND") } } DispatchQueue.main.async{ self.tableView.reloadData() } self.removeLoadingScreen() }) //}) } } } })}and snap.keys are in print("snap.key:(snap.key)"):snap.key:-KePXAuTADuTLTsZ7qbesnap.key:-KePjqMKrARQmp-gTss-snap.key:-KePsCS5rB4lTwy6GWeasnap.key:-KePwdRQPFFX30_GDAOKsnap.key:-KePyNVghX4MkSxI_1fxsnap.key:-KePz200QsJp6CSb3bVNIf I'm using this, then I get a snapshot:print("snap.key:\(snap.key)")DataService.ds.REF_USERS.queryOrdered(byChild: "provider").queryEqual(toValue: "email").observe(.value, with: { (snapshot) inprint("With Provider:\(snapshot.value)")})Result:With Provider:Optional({ JIbrGLdfCDUpMUOHEu7KzRye8ZK2 = { provider = email;}; })If I'm using this, then I don't get any result: print("snap.key:\(snap.key)") DataService.ds.REF_USERS.child("eventItem").child("eventOrganized").queryOrdered(byChild: snap.key).queryEqual(toValue: 1).observe(.value, with: { (snapshot) in ("With Snapshot Key:\(snapshot.value)")})Result:With Snapshot Key:Optional(<null>)Somebody could help me? What am I doing wrong? 解决方案 Right,DataService.ds.REF_USERS.child("eventItem").child("eventOrganized").queryOrdered(byChild: snap.key).queryEqual(toValue: 1).observe(.value, with: { (snapshot) in ("With Snapshot Key:\(snapshot.value)")})That doesn't work because you're missing a reference to the V9T3cEgEGPRmlkMQb32hxa5gG7L2 node.If you want all the eventOrganized nodes with a value of 1 under that key then you need to use the following query.DataService.ds.REF_USERS.child("V9T3cEgEGPRmIkMQb32hxa5gG7L2").child("eventItem").child("eventOrganized").queryOrderedByValue().queryEqual(toValue: 1).observe(.value, with: { (snapshot) in print("Snapshot Value:\(snapshot.value)")})Obviously you don't want to reference V9T3cEgEGPRmlkMQb32hxa5gG7L2 directly, you should store this key in a variable for reusability's sake (maybe you have, I can't tell in your code). I think that's what you wanted. 这篇关于Firebase queryEqual不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-06 08:03