如何修复“TypeError: req.next is not a function”错误?

时间:2021-03-15 05:24:17

标签: javascript node.js express mongoose

我正在处理一个项目,但不断收到此错误,我已尝试修复它,但无法解决?我明白,但不知道如何解决。

这里是错误:


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)

从查看错误我很确定错误在这个函数中:

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 });
                }
            }
        });
    }
});

这里是完整的代码:

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.');
});

1 个答案:

答案 0 :(得分:0)

错误的原因可能是您在 res.render 循环中多次使用 for,因此您需要将要显示的项目保存到一个对象中,然后将其传递render 函数中的选项。

看看这段代码:

app.get('/', function (req, res) {
    var data = []
    if (namee == "") {
        return 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++) {
                    let obj = {}
                    obj["item"] = foundItemList[i] // Look at this
                    obj["itemDesc"] = foundItemList[i].initem
                    data.append(obj)
                }
            }
        });
    }
    return res.render('main', { items: data }); // And then you can pass it like a array and you will enumerate it into the template engine
});