#trim not working on astro db forms?

1 messages · Page 1 of 1 (latest)

wild vectorBOT
#

Hi @crystal steeple. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:

#

The error you're encountering is due to the fact that the formData.get() method returns a FormDataEntryValue, which can be either a string or a File. The trim() method is not available on the File type, hence the error.

To resolve this, you can add a type check to ensure that the value is a string before calling trim(). Here's an example:

if (Astro.request.method === 'POST') {
  const formData = await Astro.request.formData();

  const name = formData.get('name');
  const shortDescription = formData.get('shortDescription');

  if (typeof name === 'string' && name.trim() !== '' && typeof shortDescription === 'string' && shortDescription.trim() !== '') {
    const projectData = {
      name: name.trim(),
      startDate: formData.has('startDate') ? new Date(formData.get('startDate')) : null,
      // ...
    };
    // ...
  }
}

In this code, we first get the values from the form data and store them in variables. Then, we check if these variables are of type string before calling trim() on them.

Please note that this answer is based on general JavaScript knowledge and not directly from the provided knowledge sources. For more specific information on handling form data in Astro, you can refer to the Astro documentation (https://docs.astro.build/en/recipes/build-forms#recipe).

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: