From 195b59c88dd30c64f0993ee44ff745813e8be5a2 Mon Sep 17 00:00:00 2001 From: Ben Elferink Date: Fri, 25 Dec 2020 19:18:44 +0200 Subject: [PATCH] simplified controllers, added post example --- server/controllers/controller.js | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/server/controllers/controller.js b/server/controllers/controller.js index afb0d49..785162c 100644 --- a/server/controllers/controller.js +++ b/server/controllers/controller.js @@ -1,17 +1,18 @@ +import mongoose from 'mongoose'; import Example from './../models/model.js'; -const getExamples = (request, response) => { - // what is .find() ??? ---> https://mongoosejs.com/docs/queries.html - Example.find((error, data) => { - if (error) { - console.log(`❌ ${error}`); - return response.status(404).json(error); // 404 not found + catched error -> send to client - } else { - console.log('✅ -FOUND- :', data); - return response.status(200).json(data); // 200 ok + array of documents -> send to client - } - // more about response status codes ---> https://restapitutorial.com/httpstatuscodes.html - }); -}; +export const getExamples = (request, response, next) => + Example.find() // what is .find() ??? ---> https://mongoosejs.com/docs/queries.html + .then((data) => response.status(200).json(data)) + .catch((error) => response.status(500).json(error)); -export default getExamples; +export const uploadExample = (request, response, next) => + new Example({ + _id: mongoose.Types.ObjectId(), // _id is set by default, you can remove this line + name: request.body.fieldName, // fieldName === name used on client side + }) + .save() // what is .save() ??? ---> https://mongoosejs.com/docs/api.html#document_Document-save + .then.then((data) => response.status(201).json(data)) + .catch((error) => response.status(500).json(error)); + +// more about response status codes ---> https://restapitutorial.com/httpstatuscodes.html