simplified controllers, added post example
This commit is contained in:
@@ -1,17 +1,18 @@
|
|||||||
|
import mongoose from 'mongoose';
|
||||||
import Example from './../models/model.js';
|
import Example from './../models/model.js';
|
||||||
|
|
||||||
const getExamples = (request, response) => {
|
export const getExamples = (request, response, next) =>
|
||||||
// what is .find() ??? ---> https://mongoosejs.com/docs/queries.html
|
Example.find() // what is .find() ??? ---> https://mongoosejs.com/docs/queries.html
|
||||||
Example.find((error, data) => {
|
.then((data) => response.status(200).json(data))
|
||||||
if (error) {
|
.catch((error) => response.status(500).json(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 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
|
||||||
|
|||||||
Reference in New Issue
Block a user