M
M
mr jeery2018-06-18 15:44:33
JavaScript
mr jeery, 2018-06-18 15:44:33

How to pass multiple arrays of values ​​via axios.put in mongoose?

Hello. Understanding mongo-express-react-node. Please help.
I have an application for adding cards and notes to each card.
Here's what it looks like.
5b27a5cf89ba2894052394.png
Cards and entries are stored in different arrays, cards and entries, respectively.
5b27a5e48e328695272300.png
Now only the cards array is stored in the database and loaded from it into the state.
It looks like
this

export const add = (cards) => {

  if (cards.length > 0) {
    return () => {
      axios.post(API_URL, {cards})
    }
  }

  else {
      cards = "empty"
      return () => {
          axios.post(API_URL, {cards})
      }
  }

};

export const searchDB = () => {
  return (dispatch) => {

    axios.get(`${API_URL}`)
      .then((res) =>
      {
        if (res.data[res.data.length - 1].cards == "empty") {

          dispatch({type: 'TODO_SEARCHED', payload: [] })
        }

        else {
            dispatch({type: 'TODO_SEARCHED', payload: res.data[res.data.length - 1].cards})
        }

      }
  );

  };
};

todo.schema
const todoSchema = new mongoose.Schema({
    cards: {type: Array, required: true},
    createdAt: {type: Date, default: Date.now}
});

How to also pass an array of entries ? Didn't understand axios.post() parameters

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Spirin, 2018-06-18
@jeerjmin

No need to save and then send all the data at once. It is customary to collect them only in collections from the server.
1. Each addition of a card is a POST request to add one card.
2. Each entry is a POST request to add an entry.
3. Each record or card update is a PUT/PATCH request with updated data for one record/card.
4. When deleting a card, if the records are separate, they must be deleted on the server side as well.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question