不能在同一条路线上使用两种或多种方法
Posted
技术标签:
【中文标题】不能在同一条路线上使用两种或多种方法【英文标题】:can´t make your two or more methods in same route 【发布时间】:2019-01-28 04:11:03 【问题描述】:项目链接 https://github.com/Kammikazy/project 我无法在同一条路线上获得两种或多种方法 我有代码 404 我使用 mysql nodejs 和 express 我的代码 控制器联盟
const User = require('../models/Alliances')
const findAlianca = async (connection, req, res) =>
const Allianca = await User.find(connection, req.session.user.username)
if (!Allianca)
res.status(404).send('Nenhuma cidade encontrada.');
return;
console.log("dddd");
req.session.Allianca = Allianca
res.locals.Allianca = Allianca
res.render('Administration/Alliances')
module.exports =
findAlianca
路线联盟
const express = require('express')
const router = express.Router()
const connection = require('../../Config/database')
const controllerAdmin = require('../../controllers/Administration')
const controlleruser = require('../../controllers/Alliances')
router.get('/Administration/Alliances', (req, res) => controllerAdmin.findcidade3(connection, req, res))
router.get('/Administration/Alliances/limitado', (req, res) => controlleruser.findAlianca(connection, req, res))
module.exports = app => app.use('/', router)
模特联盟
const find = (连接,用户名) =>
返回新的承诺((解决,拒绝)=>
connection.query(SELECT alianca.nome,alianca.N_membros,alianca.TAG FROM user INNER JOIN alianca ON user.cod_alianca=alianca.id WHERE user.username='$username'
, (err, result) =>
如果(错误)
拒绝(错误)
别的
解决(结果)
)
)
module.exports =
find
alliance.jade
extends layout
block title
.col-xs-6.col-xs-offset-3.col-sm-6.col-sm-offset-3
.col-sm-4(style='width:76%')
div.panel.panel-primary(style='height:50px') Alliances Page
div.panel.panel-primary(style='height:700px') fdssdklfsdklfjskldfjkldsjfl
if locals.user.cod_alianca==null
p You Dont Have Alliances
else
br
span Your Aliance
span= locals.Allianca.nome
.col-xs-2.panel-red(style='width:24%;height:100%;text-align:center')
我的应用
require('./routes/Administration/Alliances')(app)
我的连接数据库
const mysql = require('mysql')
const config = require( "./config.json" )
const connection =mysql.createConnection(
host:config.host,
user:config.user,
password:config.password,
database:config.database,
// port:config.port
);
connection.connect((err) =>
if(err)
console.log(err)
process.exit(0)
else
console.log('database on')
)
我做错了什么,我找不到解决问题的方法
【问题讨论】:
【参考方案1】:不确定您在问什么,但是如果您想在同一路由/API 中调用多个函数,您可以执行以下操作:
使用 expressJs 你可以使用next
函数,比如:
app.get('/Administration/Alliances', (req, res, next) =>
//Do something here and to add data to your request use
req.body.newData = 'newData';
//after this just call next function
next();
, (req, res, next) =>
//Can continue this cycle of calling next function until last `sendResponse` function is reached.
//Can even set `error` in request for `sendResponse`
req.error = "Some error";
next();
, (req, res) =>
if(req.error)
res.status(400).send(req.error);
else
res.status(200).send(req.body.result);
);
【讨论】:
我会像这样 app.get('/Administration/Alliances', (req, res, next) => req.body.newData = 'newData'; next() ; , (req, res, next) => controllerAdmin.findcidade3(connection, req, res)); controlleruser.findAlianca(connection, req, res)); req.error = "一些错误";下一个(); , (req, res) => if(req.error) res.status(400).send(req.error); 其他 res.status(200).send(req.body.result); ); 从你的回调函数调用next
函数到一个异步函数,比如: function makeDbCall (req, res, next) //make db call。 db.query('选择查询') .then ( result => req.body.result = result; next(); ) .catch( err => req.error = err; next(); ); app.get('/Administration/Alliances', makeDbCall, otherFunction, responseFunction);
你能把所有的机会都给我吗
请分享代码,这样我可以更好地帮助你。我无法理解您在寻找什么。所有这些(req、res、next)都是你的函数
知道了。你看到 router.get('/Administration/Alliances', (req, res) => controllerAdmin.findcidade3(connection, req, res));你不需要 (req, res) => 因为你在这个函数中什么都不做,只是调用 controllerAdmin.findcidade3 。另外,我希望您在调用 controllerAdmin.findcidade3 时注意到您正在传递 3 个参数连接、req、res,但是您的 (req, res) => 函数只有 2 个参数,因此您在 findcidade3
中的 res 是 undefined
。这应该类似于 router.get('/Administration/Alliances', controllerAdmin.findcidade3);【参考方案2】:
我的问题的解决方案
const express = require('express')
const router = express.Router()
const connection = require('../../Config/database')
const controllerAdmin = require('../../controllers/Administration')
const controlleruser = require('../../controllers/Alliances')
router.get('/Administration/Alliances', (req, res, next) =>
//Do something here and to add data to your request use
controllerAdmin.findcidade3(connection, req, res)
next();
, (req, res, next) =>
//Can continue this cycle of calling next function until last `sendResponse` function is reached.
//Can even set `error` in request for `sendResponse`
controlleruser.findAlianca(connection, req, res)
)
module.exports = app => app.use('/', router)
【讨论】:
以上是关于不能在同一条路线上使用两种或多种方法的主要内容,如果未能解决你的问题,请参考以下文章
在同一应用程序上使用 Passport Local 和 JWT 策略(在同一条路线上)