【问题标题】:How do i fix "TypeError: req.next is not a function" error?如何修复“TypeError:req.next 不是函数”错误?
【发布时间】:2021-06-12 09:51:54
【问题描述】:

我正在做一个项目,我一直收到这个错误,我已经尝试修复它但我不能?我理解它,但不知道如何解决它。

这是错误的:


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

【问题讨论】:

  • 你有没有机会重构你的代码只调用一次res.render?我很确定在 for 循环中多次调用它是导致错误的原因。
  • 哦,是的,谢谢!我会试试看它是否有效。
  • 另外不要忘记在if (err) {... 中添加某种渲染。否则,如果Item.find 抛出错误,您将不会渲染任何内容,页面只会挂起。
  • 好的,谢谢它解决了这个问题!非常感谢!哦,在我完成这个项目之后,我将在我们的网站或 Azi Tec 团队网站上发布它。如果您想查看并使用它,那么这里是指向我们网站azitecteam.herokuapp.com 的链接,如果您在哪里可以访问网络应用程序,那么该应用程序应该在本周末之前出现。
  • 这个项目叫做 App Lin Planner,目的是帮助规划应用程序或网站或任何编程。如果您想查看它们,我们也有其他应用程序。

标签: javascript node.js express mongoose


【解决方案1】:

错误的原因可能是您在for 循环中多次使用res.render,因此您需要将要显示的那些项目保存到一个对象中,并将其传递给选项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
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-12-25
    • 2019-07-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-26
    • 2019-07-10
    • 2020-07-25
    • 2021-01-10
    相关资源
    最近更新 更多