首页 > 解决方案 > 为什么我的对象的值是函数而不是字符串?

问题描述

我有一个事件的更新功能。用户可能已添加或未添加预告视频。如果他们有我想上传它并使用下载 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。

标签: javascriptreactjsstringfirebaseobject

解决方案


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) => {

推荐阅读