gpt4 book ai didi

node.js - 如何从 next() 停止 Express.js 路由?

转载 作者:搜寻专家 更新时间:2023-10-31 23:06:17 26 4
gpt4 key购买 nike

我有一个奇怪的情况...

我有一个 Express.js、Node.js 和 Mongoose 网络应用程序。

其中一个路由有一个调用 respond.send(...) 的 mongoose 回调。但是,因为回调之后没有别的东西,我怀疑它会自动转到 next() 路由。

例如:

//getItem 
app.get('/api/ItemOne', isUserValid, routes.getItem);
//getAnotherItem
app.get('/api/getAnotherItem', isUserValid, routes.getAnotherItem);

//Routes
exports.getItem = function (req, res) {
//console.log ('In getItem');
getItem .findOne({ some_id : some_id}, function(err, getItem ){
//console.log ('In getItem callback');
res.send({
itemName : getItem .itemName,
itemValue : getItem .itemValue;
});
})
});

exports.getAnotherItem = function (req, res) {
//console.log ('In getAnotherItem');
getAnotherItem.findOne({ some_id : some_id}, function(err, getAnotherItemRet){
//console.log ('In getAnotherItem Callback');
res.send({
itemName : getAnotherItemRet.itemName,
itemValue : getAnotherItemRet.itemValue;
});
})
});

我在控制台上收到以下消息序列...

In getItem
In getAnotherItem
In getItem callback
In getAnotherItem callback

我假设因为路由没有完成,它会自动调用 next()。

问:如何防止第二条路由被自动调用

最佳答案

尝试反转reqres:

// wrong
exports.getItem = function (res, req) {
// right
exports.getItem = function (req, res) {

(对于 getAnotherItem 也是如此)。

关于node.js - 如何从 next() 停止 Express.js 路由?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16134499/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com