0

I'm trying to post multiple values for tag inside an array of my fetch.

However, it is posting them connected in one array.

I would like it to always post it

how tag currently sent

tag:[{name: [null, null]}]

how tag should be sent

tag:[ {name: "value from field"}, {name: "next value from field"} ]

I also welcome suggestions on a more clean/efficient way to do this form.

here is my code:

class AddNew extends React.Component {
  constructor(props) {
    super(props);
    this.onTitleChange = this.onTitleChange.bind(this);
    this.onTagChange = this.onTagChange.bind(this);
    this.handleSubmit = this.handleSubmit.bind(this);
    this.state = {
      options: []
    };
  }
  componentDidMount() {
    fetch(TAG_API, {
    })
      .then(response => response.json())
      .then(json => {
        this.setState({ options: json });
      });
  }
  onTitleChange(e) {
    this.setState({ [e.target.name]: e.target.value });
    console.log("the title has changed" + e);
  }
  onTagChange(value) {
    this.setState({ value: value });
    console.log("they look like this" + value);
  }
  handleSubmit(e, value) {
    e.preventDefault();
    return (
      fetch(CREATE_API, {
        method: "POST",
        body: JSON.stringify({
          title: this.state.itemtitle,
          tag: [
            {
              name: this.state.value.map(e => {
                e.name;
              })
            }
          ]
        })
      })
        .then(res => res.json())
        .catch(err => console.error(err))
    );
  }
  render() {
    return (
      <div>
        <form onSubmit={this.handleSubmit}>
          <textarea
            name="itemtitle"
            type="text"
            placeholder="Add new..."
            onChange={this.onTitleChange}
          />
          <button type="submit">Save</button>
        </form>
        <Select
          mode="tags"
          name="tagfield"
          onChange={this.onTagChange}
        >
          {this.state.options.map(e => (
            <Option value={e.name}> {e.name} ({e.taglevel}) </Option>
          ))}

        </Select>
      </div>
    );
  }
}

1 Answer 1

1

In your handleSubmit function, your mapping function over this.state.value is never returning a value (see longer explanation later). Even if it did return, you're only ever going to have an array with a single element with all the name values. Try changing the body composition into:

body: JSON.stringify({
  title: this.state.itemtitle,
  tag: this.state.value.map(e => ({ name: e.name })),
})

Longer explanation: Arrow functions implicitly return, unless you surround the body in curly brackets, which in that case are interpreted as a function block and you need to use explicit return. By surrounding a literal object in round brackets, you can implicitly return one.

The following two examples are equivalent:

const beep = () => {
   return { boop: 'bzzt' };
};

const beep = () => ({ boop: 'bzzt' });
Sign up to request clarification or add additional context in comments.

7 Comments

Thanks for that. strangely though, that is returning empty values "tag:[{}, {}]". Ah I got it- because there is no 'name' field on 'e'. I changed e.name to just e
That's probably because you assign e.name to Option, instead of the entire object. Without knowing how Select and Option are implemented, I can only speculate.
How would I assign the entire object to it?
There are many possibilities, but the simplest example would be <Option value={e}>.
Yes I tried that, but it did not work. That gives me one result with all my values in it, instead of several
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.