M
M
mr jeery2018-07-13 19:49:27
Node.js
mr jeery, 2018-07-13 19:49:27

How to send a request from the front to change the database?

On the front, I have a React/Redux application for adding cards, and each card has an unlimited number of entries.
The Redux store has an array of cards and entries, and the usual actions: addCard, deleteCard, editCard, addEntry, deleteEntry, editEntry.

const rootReducer = combineReducers({
  cards: CardReducer,
  entries: EntryReducer,
});

On the back of express/mongoose
const todoSchema = new mongoose.Schema({
    cards: {type: Array, required: true},
    entries: {type: Array, required: true},
    createdAt: {type: Date, default: Date.now}
});

Now I use one action creator to load into the database, which passes an array with arrays of cards and entries to the database array and is executed when the main component is rendered.
axios.post(API_URL, {cards, entries})
And in componentDidMount, I call the action creator, which accesses the database and gets the last array in the database array.
Here is the question. I want to do it right... I
guess I need to create function requests to the back for each case. But how ? Like this:
addCardBD - add a card and on the back one object with an empty "name" field will be added to the cards array.
editCardBD - the 'name' field will change on the back.
deleteCardBD
etc
Code link: https://github.com/Jeerjmin/dnd-mern
If something is not clear or if I add more code, I will correct it and add it.
Thanks

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
mr jeery, 2018-07-14
@jeerjmin

No one answers, I will answer myself.
Came to express.Router.
I found such a solution on gith, poking around here
https://github.com/cefjoeii/mern-crud

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question