I'm having some trouble sending any array from my redux's store as form data. My back-end works using Postman to send an array, I guess I'm just having some problems connecting my front-end to it. My front-end code basically uses form input to update local state, which ideally gets sent in. This is my code below.client/src/actions/customersAction.js// Post New Customer export const postCustomer = newTable => dispatch => { axios .post("/customers", newTable) .then(res => { dispatch({ type: POST_NEW_CUSTOMERS, payload: res.data }); }) .catch(err => { alert(err.message); }); }; server/routes/customers.js // Post new customer app.post("/customers", (req, res) => { const { newTable } = req.body; const { customer_name, customer_phone } = newTable; // POST single customer if (newTable.length < 1) { db.Customers.create({ customer_name, customer_phone }) .then(customer => { console.log("> New Customer Created"); console.log(JSON.stringify(customer)); }) .catch(err => { console.log(err.message); }); } // POST more than one customer else { db.Customers.bulkCreate(newTable) .then(customers => { res.json(customers); }) .then( () =>{ console.log('> Multiple customers created.') }) .catch(err => { console.log(err.message); }); } }); /server.js (partial-code)const app = express(); const PORT = process.env.PORT || 5000; const db = require("./server/config/database"); const router = require("./server/routes/index"); app.use(express.urlencoded({ extended: true })); app.use(express.json()); app.use(cors()); app.use((req, res, next) => { res.header("Content-Type", "application/json"); next(); }); Postman example that works{ "newTable":[ { "customer_name":"name", "customer_phone":"1111111111" } ] }
Submitted December 27, 2019 at 06:57PM by kingducasse
No comments:
Post a Comment