firebase - 图像存储在firestore中,但仍在浏览器中显示firebase错误(不支持的字段值:未定义)?
问题描述
在浏览器上反应错误:
Unhandled Rejection (FirebaseError): Function addDoc() called with invalid data. Unsupported field value: undefined (found in field username in document posts/MkHzQWzXyayty0KfQQgP)
很可能问题出在这段代码中:
// complete function
storage
.ref("images")
.child(image.name)
.getDownloadURL()
.then(url => {
// Post image on db
db.collection("posts").add({
timestamp: firebase.firestore.FieldValue.serverTimestamp(),
caption: caption,
imageUrl: url,
username: username
});
setProgress(0);
setCaption("");
setImage(null);
});
}
);
}; ```
解决方案
In the error, it says your variable username
is undefined
. You should make sure that it has a value. Meanwhile, you can do a quick and dirty fix like this:
db.collection("posts").add({
timestamp: firebase.firestore.FieldValue.serverTimestamp(),
caption: caption,
imageUrl: url,
username: username || null,
});
This code will assign null to username when the variable username is undefined.
推荐阅读
- java - HotSwapAgent 无法将 filePath 转换为 classPathPath
- php - Laravel 应用到 Phar
- python - 如何使用 python 检查并在文件的每一行中插入缺失的变量?
- firebase - Firebase 动态链接不会在社交媒体中预览图片
- variables - Visual-C++ Win32 C++ 应用程序。如何将变量打印到主屏幕?
- java - 当我将 0 加到一个数字上时,结果始终为零。其他数字没问题
- reactjs - 可以连接 PureComponent 吗?
- ffmpeg - ffmpeg 可以等到新的输入吗?
- linux - 在 Linux 服务器上运行 mongodb 时出错
- c# - 在 ASP.NET 核心中绑定源参数推断