javascript - 为什么我的对象的值是函数而不是字符串?
问题描述
我有一个事件的更新功能。用户可能已添加或未添加预告视频。如果他们有我想上传它并使用下载 url 保存该对象。
我使用不同的功能来上传视频,并且仅在用户附加视频时才调用它。
但是当我尝试更新时,它告诉我我正在尝试编写一个无效对象,因为其中的数据teaser
是一个函数,我希望它是一个字符串(下载 url,或者只是一个空字符串。
我究竟做错了什么?
这就是我调用函数的方式:
updateEvent(values, videoAsFile, () => {
setIsEventEdited(false);
setCurrentEvent(values);
})
然后是这些功能:
const uploadTeaserVideo = (event, video) => async () => {
const ref = storage.ref(`/videos/events/${event.id}/`);
const upload = await ref.put(video);
if (!upload) return "";
const downloadUrl = await ref.getDownloadURL();
if (!downloadUrl) return "";
return downloadUrl;
};
export const updateEvent = (values, teaserVideo, cb) => async () => {
if (teaserVideo) {
const teaser = await uploadTeaserVideo(values, teaserVideo);
db.collection("events")
.doc(values.id)
.set({ ...values, teaser })
.then(() => {
cb();
});
} else {
db.collection("events")
.doc(values.id)
.set(values)
.then(() => {
cb();
});
}
};
我已检查,并且teaserVideo
是有效的视频文件,如果未选择视频,则为 null。
解决方案
uploadTeaserVideo
定义为返回函数的函数:
// vv−−−−−−−−−−−−−−− Start of the uploadTeaserVideo function body
const uploadTeaserVideo = (event, video) => async () => {
// ^^−−− Start of the body of the function it returns
const ref = storage.ref(`/videos/events/${event.id}/`);
const upload = await ref.put(video);
if (!upload) return "";
const downloadUrl = await ref.getDownloadURL();
if (!downloadUrl) return "";
return downloadUrl;
};
我怀疑你的意思是它是一个async
返回(承诺)的函数downloadUrl
:
const uploadTeaserVideo = async (event, video) => {
推荐阅读
- sql - 我们需要提供排名。逻辑是,每当我们有一个值“产品”时,我们必须从新数字开始,下面的所有其他数字都应该遵循相同的排名
- r - 在R中的混合字符串中删除破折号后的字符
- woocommerce - 如何在我的所有网站中仅显示库存产品?Woocommerce + WordPress
- amazon-web-services - 从 CLI 删除 CodeArtifact 版本
- python - 使用库线程或多处理修改脚本
- scheme - 无法从 Brian Harvey 的 CS61a 2010 加载“obj.scm”
- python - 什么时候类应该成为主要使用它的模块的一部分?
- sequelize.js - Sequelize 中的原始查询
- python - flask-apispec 没有使用来自 GET 查询的值填充 kwargs(文档中示例代码的实现)
- python - 在嵌套列表中拆分字符串