I am working on a project and i keep getting this error, and i have tried fixing it but i can't? I understand it but don't know how to fix it.
Here is the err:
events.js:292
throw er; // Unhandled 'error' event
^
TypeError: req.next is not a function
at done (C:\Users\Children\Desktop\Web Projects\App Line Planner\node_modules\express\lib\response.js:1007:25)
at tryRender (C:\Users\Children\Desktop\Web Projects\App Line Planner\node_modules\express\lib\application.js:642:5)
at Function.render (C:\Users\Children\Desktop\Web Projects\App Line Planner\node_modules\express\lib\application.js:592:3)
at ServerResponse.render (C:\Users\Children\Desktop\Web Projects\App Line Planner\node_modules\express\lib\response.js:1012:7)
at C:\Users\Children\Desktop\Web Projects\App Line Planner\main.js:48:25
at C:\Users\Children\Desktop\Web Projects\App Line Planner\node_modules\mongoose\lib\model.js:4866:16
at C:\Users\Children\Desktop\Web Projects\App Line Planner\node_modules\mongoose\lib\model.js:4866:16
at C:\Users\Children\Desktop\Web Projects\App Line Planner\node_modules\mongoose\lib\helpers\promiseOrCallback.js:24:16
at C:\Users\Children\Desktop\Web Projects\App Line Planner\node_modules\mongoose\lib\model.js:4889:21
at C:\Users\Children\Desktop\Web Projects\App Line Planner\node_modules\mongoose\lib\query.js:4400:11
at C:\Users\Children\Desktop\Web Projects\App Line Planner\node_modules\kareem\index.js:136:16
at processTicksAndRejections (internal/process/task_queues.js:79:11)
Emitted 'error' event on Function instance at:
at C:\Users\Children\Desktop\Web Projects\App Line Planner\node_modules\mongoose\lib\model.js:4868:13
at C:\Users\Children\Desktop\Web Projects\App Line Planner\node_modules\mongoose\lib\helpers\promiseOrCallback.js:24:16
[... lines matching original stack trace ...]
at processTicksAndRejections (internal/process/task_queues.js:79:11)
And from looking at the error i am pretty sure that the error is in this function:
app.get('/', function (req, res) {
if (namee == "") {
res.redirect('/login');
} else {
Item.find({ name: namee }, function (err, foundItemList) {
if (err) {
console.log(err);
} if (foundItemList) {
console.log(namee);
console.log("Found ITems: " + foundItemList);
for (var i = 0; i < foundItemList.length; i++) {
res.render('main', { item: foundItemList, foundItems: foundItemList[i].initem });
}
}
});
}
});
And here is the full code:
const express = require('express');
const bodyParser = require('body-parser');
const ejs = require('ejs');
const mongoose = require('mongoose');
mongoose.connect("mongodb://localhost:27017/appline", { useUnifiedTopology: true }, { useNewUrlParser: true });
const itemSchema = {
name: String,
initem: Array,
text: String,
tab: Number
}
const Item = mongoose.model("Item", itemSchema);
const app = express();
app.set('view engine', 'ejs');
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.static('public'));
const items = [];
const tabs = [];
var namee = "";
var tabb = 0;
app.get('/login', function (req, res) {
res.render('login');
});
app.post('/login', function (req, res) {
namee = req.body.name;
res.redirect('/');
})
app.get('/', function (req, res) {
if (namee == "") {
res.redirect('/login');
} else {
Item.find({ name: namee }, function (err, foundItemList) {
if (err) {
console.log(err);
} if (foundItemList) {
console.log(namee);
console.log("Found ITems: " + foundItemList);
for (var i = 0; i < foundItemList.length; i++) {
res.render('main', { item: foundItemList, foundItems: foundItemList[i].initem });
}
}
});
}
});
app.post('/addinitem', function (req, res) {
tabb += 1;
tex = req.body.addtoitem;
const item2 = new Item({
name: namee,
initem: [],
text: tex,
tab: tabb
});
Item.find({ name: namee }, function (err, foundITems) {
if (err) {
console.log(err);
} else if (foundITems) {
foundITems.initem.push(item2);
foundITems.save(function () {
res.redirect('/');
});
}
});
});
app.post('/add', function (req, res) {
const item = req.body.newItem;
console.log(item);
items.push(item);
const item1 = new Item({
name: namee,
initem: [],
text: item
});
item1.save();
console.log(item1);
res.redirect('/');
});
app.listen(3000, function () {
console.log('Server started on port 3000.');
});