swift - 挣扎于基本的 Swift 逻辑(使用常量、变量和循环)
问题描述
我想使用从 firebase 获得的两个“循环”中的 2 个字符串,并在另一个“循环”中使用它们以将它们与一堆其他信息一起上传。
我的问题是,我无法将下载的值fullname
和pfp
下载的值上传到firebase。
关于如何解决这个问题的任何想法?
func sendToFire(){
let combined = "\(userID)" + "\(number)"
let docRef = db.collection("posts").document(combined)
let description = self.textPost.text
let nameRef = db.collection("users").document(userID)
var fullname = ""
var pfp = ""
if fireImage == nil {
nameRef.getDocument { (document, error) in
if let document = document{
fullname = document.get("fullname") as! String
}else{
print("Coulnt get fullname")
}
}
nameRef.getDocument { (document, error) in
if let document = document{
pfp = document.get("profileimage") as! String
}else{
print("Couldn't get profileimage")
}
}
docRef.getDocument { (document, error) in
if let document = document, document.exists {
print("Post ID already taken")
} else {
print("Post Document gets created")
self.db.collection("posts").document(combined).setData([
"description": description!,
"likes": self.likes,
"postType": 0,
"profileImage": pfp,
"time": self.date,
"uid": self.userID,
"username": fullname
]) { err in
if let err = err {
print("Error writing document: \(err)")
} else {
print("Post Document successfully written!")
}
}
}
}
}
}
解决方案
加入document.exists
if let
nameRef.getDocument { (document, error) in
if let document = document, document.exists{
fullname = document.get("fullname") as! String
}else{
print("Coulnt get fullname")
}
}
nameRef.getDocument { (document, error) in
if let document = document, document.exists{
pfp = document.get("profileimage") as! String
}else{
print("Couldn't get profileimage")
}
}
检查响应中的实际键名fullname
和profileimage
.
推荐阅读
- sql - 如何在 SQL 中使检查约束不区分大小写?
- mysql - 使用向导创建表后如何从 MySQL Workbench 导出 DDL?
- javascript - Discord.js - TypeError:无法读取未定义的属性(读取“设置”)
- flutter - labelText 对于 dropdownbuttonfield/textformfield 来说太大了。如何显示所有文本?
- r - 在 Rstudio 中添加两个不同表的两列
- regular-language - 构建正则表达式
- r - 有没有办法从向下滚动修复顶部标题、按钮和搜索框
- postgresql - 如何将 pgadmin 容器连接到主机系统 localhost postgres
- flutter - 轮播滑块导致 GestureDetector 延迟
- r - 尽管块已成功运行,但当我在 RMD 中按 knit 时出错