I am simply trying to increment a Mongoose User model every time a button gets clicked in my dashboard.ejs and am running into trouble. This is my first time creating express routes so any help would be great!
You can view the terminal error below. I am only showing the method in the controller which I am working on right now, but the conrtoller file is actually much larger.
the req.user object is returning properly in the controller and returns the current user that is logged in
userRoutes.js
var express = require('express');
var router = express.Router();
const controller = require('./userController');
router.route('/login')
.get(controller.getLogin)
.post(controller.postLogin)
router.route('/register')
.get(controller.getRegister)
.post(controller.postRegister)
router.route('/logout')
.get(controller.getLogout)
router.route('/buyBullet')
.get(controller.buyBullet);
module.exports = router;
userController.js
exports.buyBullet = (req, res, next) => {
User.findByIdAndUpdate(req.user._id, { $inc: { bullets: 1 }})
User.save();
console.log(req.user)
res.redirect('/dashboard');
}
dashboard.ejs
<%- include('partials/header'); %>
<a href="/users/logout" class="btn btn-secondary">Logout</a>
<a href="/users/buyBullet" class="btn btn-warning">Buy Bullet</a>
<div class="container">
<div class="col-md-8 m-auto">
<ul class="nav justify-content-center mt-5 mb-5">
<li class="nav-item">
<a class="nav-link"href="#">Make Your Pick</a>
</li>
<li class="nav-item">
<a class="nav-link"href="#">Dashboard</a>
</li>
<li class="nav-item">
<a class="nav-link"href="https://www.nfl.com/schedules/" target="_blank">Schedule</a>
</li>
<li class="nav-item">
<a class="nav-link"href="#">Rules</a>
</li>
<li class="nav-item">
<a class="nav-link"href="#">Message Board</a>
</li>
</ul>
</div>
<div class="col-md-6 m-auto">
<div class="card card-body">
<h1 class="text-center mb-3">Dashboard</h1>
<p>The Date is now <b><%= new Intl.DateTimeFormat('en-GB', { year: 'numeric', month: 'long', day: '2-digit'}).format(new Date()) %></b>
and the time is now <b><%= new Date().toLocaleTimeString() %></b></p>
</div>
</div>
</div>
error
Error: Failed to lookup view "error" in views directory "/Users/jojovera/Documents/fantasyfootball/views"
at Function.render (/Users/jojovera/Documents/fantasyfootball/node_modules/express/lib/application.js:580:17)
at ServerResponse.render (/Users/jojovera/Documents/fantasyfootball/node_modules/express/lib/response.js:1008:7)
at ServerResponse.res.render (/Users/jojovera/Documents/fantasyfootball/node_modules/express-ejs-layouts/lib/express-layouts.js:77:18)
at /Users/jojovera/Documents/fantasyfootball/app.js:74:7
at Layer.handle_error (/Users/jojovera/Documents/fantasyfootball/node_modules/express/lib/router/layer.js:71:5)
at trim_prefix (/Users/jojovera/Documents/fantasyfootball/node_modules/express/lib/router/index.js:315:13)
at /Users/jojovera/Documents/fantasyfootball/node_modules/express/lib/router/index.js:284:7
at Function.process_params (/Users/jojovera/Documents/fantasyfootball/node_modules/express/lib/router/index.js:335:12)
at next (/Users/jojovera/Documents/fantasyfootball/node_modules/express/lib/router/index.js:275:10)
at Layer.handle_error (/Users/jojovera/Documents/fantasyfootball/node_modules/express/lib/router/layer.js:67:12)
at trim_prefix (/Users/jojovera/Documents/fantasyfootball/node_modules/express/lib/router/index.js:315:13)
at /Users/jojovera/Documents/fantasyfootball/node_modules/express/lib/router/index.js:284:7
at Function.process_params (/Users/jojovera/Documents/fantasyfootball/node_modules/express/lib/router/index.js:335:12)
at next (/Users/jojovera/Documents/fantasyfootball/node_modules/express/lib/router/index.js:275:10)
at Layer.handle_error (/Users/jojovera/Documents/fantasyfootball/node_modules/express/lib/router/layer.js:67:12)
at trim_prefix (/Users/jojovera/Documents/fantasyfootball/node_modules/express/lib/router/index.js:315:13)
GET /users/buyBullet 500 60.356 ms - 2220
question from:
https://stackoverflow.com/questions/66059491/findbyidandupdate-model-and-increment-based-off-button-click 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…