TypeError: Object (...) не является функцией реакции S3 - PullRequest
1 голос
/ 02 ноября 2019

В настоящее время я ищу, чтобы добавить форму, в которой я могу создать запись в своей БД и загрузить изображения на S3, однако я сталкиваюсь с

TypeError: Object(...) is not a function
2 New.js:161
SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data

Вот мой код

import React, { Component } from "react";
import { uploadFile } from "aws-s3";

const initalState = {
  title: "",
  description: "",
  reference: "",
  images: [],
  price: "",
  year: "",
  category: "",

  titleErr: "",
  descriptionErr: "",
  referenceErr: "",
  imagesErr: "",
  priceErr: "",
  yearErr: "",
  categoryErr: ""
};

class New extends Component {
  constructor() {
    super();
    this.state = {
      initalState,
      categorys: [],
      successMsg: false
    };

    this.s3Config = {
      bucketName: "REMOVED",
      dirName: "",
      region: "eu-west-1",
      accessKeyId: "REMOVED",
      secretAccessKey: "REMOVED"
    };

    this.handleSubmit = this.handleSubmit.bind(this);
  }

  validate = () => {
    let titleErr = "";
    let descriptionErr = "";
    let referenceErr = "";
    let imagesErr = "";
    let priceErr = "";
    let yearErr = "";
    let categoryErr = "";

    // Title validation
    if (!this.state.title) {
      titleErr = "Please enter a title";
    }

    // Description error
    if (!this.state.description) {
      descriptionErr = "Please enter a description";
    }

    // Reference error
    if (!this.state.reference) {
      referenceErr = "Please enter a reference";
    }

    // Images validation
    if (!this.state.images) {
      imagesErr = "You must have at least one image";
    }

    // Price validation
    if (!this.state.price) {
      priceErr = "Please enter a price";
    }

    // Year validation
    if (!this.state.year) {
      yearErr = "Please enter a year";
    }

    // Category year
    if (!this.state.category) {
      categoryErr = "Please select a category";
    }

    if (
      titleErr ||
      descriptionErr ||
      referenceErr ||
      imagesErr ||
      priceErr ||
      yearErr ||
      categoryErr
    ) {
      this.setState({
        titleErr,
        descriptionErr,
        referenceErr,
        imagesErr,
        priceErr,
        yearErr,
        categoryErr
      });
      return false;
    }

    return true;
  };

  onTitleChange(event) {
    this.setState({ title: event.target.value });
  }

  onDescriptionChange(event) {
    this.setState({ description: event.target.value });
  }

  onReferenceChange(event) {
    this.setState({ reference: event.target.value });
  }

  onImagesChange(event) {
    this.setState({ images: event.target.value });
  }

  onPriceChange(event) {
    this.setState({ price: event.target.value });
  }

  onYearChange(event) {
    this.setState({ year: event.target.value });
  }

  onCategoryChange(event) {
    this.setState({ category: event.target.value });
  }

  componentDidMount() {
    fetch("/api/category")
      .then(res => res.json())
      .then(categorys => this.setState({ categorys }))
      .catch(function(error) {
        console.log(error);
      });
  }

  handleSubmit(event) {
    event.preventDefault();
    const isValid = this.validate();
    const {
      title,
      description,
      reference,
      images,
      price,
      year,
      category
    } = this.state;

    if (isValid) {
      fetch("/api/collections/create", {
        method: "POST",
        body: JSON.stringify(
          title,
          description,
          reference,
          images,
          price,
          year,
          category
        ),
        headers: {
          Accept: "application/json",
          "Content-Type": "application/json"
        }
      })
        .then(res => res.json())
        .then(uploadFile(this.state.images, this.s3Config))
        .then(this.setState({ successMsg: true }), this.setState(initalState))
        .catch(error => {
          console.log(error);
        });
    }
  }

  render() {
    const {
      title,
      description,
      reference,
      images,
      price,
      year,
      category
    } = this.state;
    return (
      <>
        <div className='p-12 w-full text-center text-gray-800'>
          <h1 className='title mb-10'>Create a collection item</h1>

          {this.state.successMsg && (
            <div
              className='w-full m-auto max-w-lg mb-10 bg-green-100 border border-green-400 text-green-700 px-4 py-3 rounded relative'
              role='alert'
            >
              <strong className='font-bold'>Holy smokes! </strong>
              <span className='block sm:inline'>
                You have just added a new collection item.
              </span>
            </div>
          )}

          <form className='w-full m-auto max-w-lg'>
            <div className='flex flex-wrap -mx-3 mb-6'>
              <div className='w-full md:w-1/2 px-3 mb-6 md:mb-0'>
                <label htmlFor='title'>Title</label>

                <input
                  id='title'
                  type='text'
                  placeholder='Enter a title here'
                  value={title || ""}
                  onChange={this.onTitleChange.bind(this)}
                />

                <p className='my-2 text-red-500 text-xs'>
                  {this.state.titleErr}
                </p>
              </div>

              <div className='w-full md:w-1/2 px-3'>
                <label htmlFor='reference'>Reference</label>

                <input
                  id='reference'
                  type='text'
                  placeholder='Enter a year here'
                  value={reference || ""}
                  onChange={this.onReferenceChange.bind(this)}
                />

                <p className='my-2 text-red-500 text-xs'>
                  {this.state.referenceErr}
                </p>
              </div>
            </div>

            <div className='flex flex-wrap -mx-3 mb-6'>
              <div className='w-full md:w-1/2 px-3 mb-6 md:mb-0'>
                <label htmlFor='year'>Year</label>

                <input
                  id='year'
                  type='number'
                  placeholder='Enter a year here'
                  value={year || ""}
                  onChange={this.onYearChange.bind(this)}
                />

                <p className='my-2 text-red-500 text-xs'>
                  {this.state.yearErr}
                </p>
              </div>

              <div className='w-full md:w-1/2 px-3 mb-6 md:mb-0'>
                <label htmlFor='category'>Category</label>

                <div className='relative'>
                  <select
                    id='category'
                    value={category || ""}
                    onChange={this.onCategoryChange.bind(this)}
                  >
                    <option>N/A</option>
                    {this.state.categorys.map(category => (
                      <option key={category._id} value={category.name}>
                        {category.name}
                      </option>
                    ))}
                  </select>

                  <div className='pointer-events-none absolute inset-y-0 right-0 flex items-center px-2'>
                    <svg
                      className='fill-current h-4 w-4'
                      xmlns='http://www.w3.org/2000/svg'
                      viewBox='0 0 20 20'
                    >
                      <path d='M9.293 12.95l.707.707L15.657 8l-1.414-1.414L10 10.828 5.757 6.586 4.343 8z' />
                    </svg>
                  </div>

                  <p className='my-2 text-red-500 text-xs'>
                    {this.state.categoryErr}
                  </p>
                </div>
              </div>
            </div>

            <div className='flex flex-wrap -mx-3 mb-6'>
              <div className='w-full md:w-1/2 px-3 mb-6 md:mb-0'>
                <label htmlFor='price'>Price</label>

                <input
                  id='price'
                  type='number'
                  placeholder='Enter a price here'
                  value={price || ""}
                  onChange={this.onPriceChange.bind(this)}
                />

                <p className='my-2 text-red-500 text-xs'>
                  {this.state.priceErr}
                </p>
              </div>

              <div className='w-full md:w-1/2 px-3'>
                <label htmlFor='reference'>Images (Multiple allowed)</label>

                <input
                  id='reference'
                  type='file'
                  value={images || ""}
                  onChange={this.onImagesChange.bind(this)}
                />

                <p className='my-2 text-red-500 text-xs'>
                  {this.state.imagesErr}
                </p>
              </div>
            </div>

            <div className='flex flex-wrap -mx-3'>
              <div className='w-full px-3'>
                <label htmlFor='description'>Description</label>

                <textarea
                  id='description'
                  type='text'
                  placeholder='Enter a description here'
                  value={description || ""}
                  onChange={this.onDescriptionChange.bind(this)}
                />
                <p className='my-2 text-red-500 text-xs'>
                  {this.state.descriptionErr}
                </p>
              </div>
            </div>

            <div className='flex'>
              <button
                className='btn'
                type='button'
                onClick={this.handleSubmit.bind(this)}
              >
                Send
              </button>
            </div>
          </form>
        </div>
      </>
    );
  }
}

export default New;

Я получаю 400 ошибок при отправке. Форма все готово и работает, прежде чем я добавил s3, я следовал документации, поэтому не уверен, где я ошибся.

1 Ответ

3 голосов
/ 02 ноября 2019

У вас там есть несколько ошибок.

Первое, вы не правильно используете JSON.stringify . Ваш код:

JSON.stringify(
  title,
  description,
  reference,
  images,
  price,
  year,
  category
)

У вас должен быть 1 аргумент, который является объектом, который вы хотите преобразовать в строку.

Измените его следующим образом:

JSON.stringify({
  title,
  description,
  reference,
  images,
  price,
  year,
  category
})

Второй:вы не правильно используете методы обещания.

В своем коде вы выполняете uploadFile и setState сразу, а не когда разрешение обещает. Ваш код:

.then(uploadFile(this.state.images, this.s3Config))
.then(this.setState({ successMsg: true }), this.setState(initalState))

Вы должны заключить его в функцию стрелки, чтобы выполнить, когда обещание разрешается.

Попробуйте заменить его следующим:

.then(() => uploadFile(this.state.images, this.s3Config))
.then(() => this.setState({ successMsg: true }), () => this.setState(initalState))
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...