【问题标题】:Why is my object's values a function and not a string?为什么我的对象的值是函数而不是字符串?
【发布时间】:2020-06-04 13:11:51
【问题描述】:

我有一个事件的更新功能。用户可能已添加或未添加预告视频。如果他们有我想上传它并使用下载网址保存该对象。

我使用不同的函数来上传视频,并且仅在用户附加视频时才调用它。

但是当我尝试更新时,它告诉我我正在尝试写入一个无效对象,因为 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。

【问题讨论】:

  • 您是否记录了teaser 的值以查看它正在获取什么对象?这将是一种将调试搜索指向正确方向的快速方法。
  • 因为这就是你告诉uploadTeaserVideo() 返回的内容:const uploadTeaserVideo = (event, video) => async () => { ... }。它返回一个async 函数,如果执行该函数将返回一个downloadUrl(或一个空字符串)
  • @BrianThompson 是真的。我刚刚做了,它只打印了整个函数,没有返回选项。如何让它等待响应而不是把整个函数当作它的值?

标签: javascript reactjs string firebase object


【解决方案1】:

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;
};

我怀疑你的意思是它是一个返回(承诺)downloadUrlasync 函数:

const uploadTeaserVideo = async (event, video) => {

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-10-15
    • 2016-05-29
    • 2021-11-19
    • 2020-01-10
    • 2011-08-15
    • 1970-01-01
    • 2011-11-24
    • 1970-01-01
    相关资源
    最近更新 更多