Why is my object's values a function and not a string?

Viewed 43

I have an update function for an event. It is possible that the user has added a teaser video or not. If they have I want to upload it and save that object with the download url.

I use a different function to upload the video and only call it if the user attached a video.

But when I try to update, it tells me that I'm trying to write an invalid object because the data in teaser is a function, where I want it to be a string (either the download url, or just an empty string.

What am I doing wrong?

This is how I call the function:

updateEvent(values, videoAsFile, () => {
    setIsEventEdited(false);
    setCurrentEvent(values);
})

Then these are the functions:

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

I've checked, and teaserVideo is a valid video file, or null if a video wasn't chosen.

1 Answers

uploadTeaserVideo is defined as a function that returns a function:

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

I suspect you meant it to be an async function that returns (a promise of) downloadUrl:

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