Created
June 30, 2020 12:15
-
-
Save Cool-sami12/c0f4a6f0aef8b64a1a78145096b588ce to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const makeValidation = require('@withvoid/make-validation'); | |
| const UserModel, { USER_TYPES } = require('../models/user.js'); | |
| module.exports = { | |
| onGetAllUsers: async (req, res) => { | |
| try { | |
| const users = await UserModel.getUsers(); | |
| return res.status(200).json({ success: true, users }); | |
| } catch (error) { | |
| return res.status(500).json({ success: false, error: error }) | |
| } | |
| }, | |
| onGetUserById: async (req, res) => { | |
| try { | |
| const user = await UserModel.getUserById(req.params.id); | |
| return res.status(200).json({ success: true, user }); | |
| } catch (error) { | |
| return res.status(500).json({ success: false, error: error }) | |
| } | |
| }, | |
| onCreateUser: async (req, res) => { | |
| try { | |
| const validation = makeValidation(types =>({ | |
| payload: req.body, | |
| checks: { | |
| firstName: { type: types.string }, | |
| lastName: { type: types.string }, | |
| type: { type: types.enum, options: { enum: USER_TYPES } }, | |
| } | |
| })); | |
| if (!validation.success) return res.status(400).json(validation); | |
| const { firstName, lastName, type } = req.body; | |
| const user = await UserModel.createUser(firstName, lastName, type); | |
| return res.status(200).json({ success: true, user }); | |
| } catch (error) { | |
| return res.status(500).json({ success: false, error: error }) | |
| } | |
| }, | |
| onDeleteUserById: async (req, res) => { | |
| try { | |
| const user = await UserModel.deleteByUserById(req.params.id); | |
| return res.status(200).json({ | |
| success: true, | |
| message: `Deleted a count of ${user.deletedCount} user.` | |
| }); | |
| } catch (error) { | |
| return res.status(500).json({ success: false, error: error }) | |
| } | |
| }, | |
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| C:\Users\SAMUEL\Documents\codes\chatapp\chatapp\controllers\user.js:2 | |
| const UserModel, { USER_TYPES } = require('../models/user.js'); | |
| ^^^^^^^^^ | |
| SyntaxError: Missing initializer in const declaration | |
| at wrapSafe (internal/modules/cjs/loader.js:1054:16) | |
| at Module._compile (internal/modules/cjs/loader.js:1102:27) | |
| at Object.Module._extensions..js (internal/modules/cjs/loader.js:1158:10) | |
| at Module.load (internal/modules/cjs/loader.js:986:32) | |
| at Function.Module._load (internal/modules/cjs/loader.js:879:14) | |
| at Module.require (internal/modules/cjs/loader.js:1026:19) | |
| at require (internal/modules/cjs/helpers.js:72:18) | |
| at Object.<anonymous> (C:\Users\SAMUEL\Documents\codes\chatapp\chatapp\routes\user.js:7:14) | |
| at Module._compile (internal/modules/cjs/loader.js:1138:30) | |
| at Object.Module._extensions..js (internal/modules/cjs/loader.js:1158:10) | |
| at Module.load (internal/modules/cjs/loader.js:986:32) | |
| at Function.Module._load (internal/modules/cjs/loader.js:879:14) | |
| at Module.require (internal/modules/cjs/loader.js:1026:19) | |
| at require (internal/modules/cjs/helpers.js:72:18) | |
| at Object.<anonymous> (C:\Users\SAMUEL\Documents\codes\chatapp\chatapp\server\index.js:11:20) | |
| at Module._compile (internal/modules/cjs/loader.js:1138:30) | |
| [nodemon] app crashed - waiting for file changes before starting... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const express = require('express'); | |
| const router = express.Router(); | |
| //controllers | |
| const user = require('../controllers/user.js'); | |
| router | |
| .get('/', user.onGetAllUsers) | |
| .post('/', user.onCreateUser) | |
| .get('/:id', user.onGetUserById) | |
| .delete('/:id', user.onDeleteUserById) | |
| module.exports = router; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const http = require('http'); | |
| const express = require('express'); | |
| const logger = require('morgan'); | |
| const cors =require('cors'); | |
| // mongo connection | |
| require('../config/mongo.js'); | |
| //routes | |
| const userRouter = require('../routes/user.js'); | |
| /* | |
| const indexRouter = require('../routes/index.js'); | |
| const chatRoomRouter = require('../routes/chatRoom.js'); | |
| const deleteRouter = require('../routes/delete.js'); | |
| */ | |
| //middlewares | |
| const { decode } = require('../middlewares/jwt.js') | |
| const app = express(); | |
| const port = process.env.PORT || "3000"; | |
| app.set("port", port); | |
| app.use(logger("dev")); | |
| app.use(express.json()); | |
| app.use(express.urlencoded({ extended: false })) | |
| app.use("/", indexRouter); | |
| app.use("/users", userRouter); | |
| /* | |
| app.use("/room", decode, chatRoomRouter); | |
| app.use("/delete", deleteRouter); | |
| */ | |
| // catching errors/error handling | |
| app.use('*', (req,res) => { | |
| return res.status(404).json({ | |
| success:false, | |
| message: ' API endpoint doesnt exist' | |
| }) | |
| }); | |
| // creating http server | |
| const server = http.createServer(app); | |
| server.listen(port); | |
| server.on("listening", () =>{ | |
| console.log(`Listening on port :: http:/localhost:${port}/`) | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is a similar issue to the one you were facing over the weekend. Because you're using commonjs (require syntax) you're unable to use the conveniences of
import. The error you're facing is thatconst UserModel,breaks node's expectation that an=will follow it.Try changing https://gist.github.com/Cool-sami12/c0f4a6f0aef8b64a1a78145096b588ce#file-controller_user-js-L2 to: