can´t make your two or more methods in same route
can´t make your two or more methods in same route
the link to the project
https://github.com/Kammikazy/project
i can´t make work my get two or more methods in same route
i have the code 404
i using mysql nodejs and express
my code
controller alliances
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
route aliance
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)
models aliance
const find = (connection,username) =>
return new Promise((resolve, reject) =>
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) =>
if(err)
reject(err)
else
resolve(result)
)
)
SELECT alianca.nome,alianca.N_membros,alianca.TAG FROM user INNER JOIN alianca ON user.cod_alianca=alianca.id WHERE user.username='$username'
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')
my app
require('./routes/Administration/Alliances')(app)
my connection db
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')
)
what i doing wrong i can´t find the solution for my problem
2 Answers
2
Not sure what you are asking however if you want to call multiple function in same route/API you can do following:
Using expressJs you can use next
function like:
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);
);
call
next
function from your callback function to an async function, like: function makeDbCall (req, res, next) //make db call. db.query('select query') .then ( result => req.body.result = result; next(); ) .catch( err => req.error = err; next(); ); app.get('/Administration/Alliances', makeDbCall, otherFunction, responseFunction);– NAVIN
Aug 22 at 16:54
next
Can you put all chance i have to make
– Rsrd
Aug 22 at 16:58
please share code, so i can help you better. I'm not able to understand what you are looking for. all these (req, res, next) are your functions
– NAVIN
Aug 22 at 17:02
Got it. You see router.get('/Administration/Alliances', (req, res) => controllerAdmin.findcidade3(connection, req, res)); You dont need (req, res) => as you are doing nothing in this function just calling controllerAdmin.findcidade3 . Also I would like you to notice while calling controllerAdmin.findcidade3 you are passing 3 arguments connection, req, res, however your (req, res) => function only got 2 arguments so your res in
findcidade3
is undefined
. This should be like router.get('/Administration/Alliances', controllerAdmin.findcidade3);– NAVIN
Aug 22 at 17:27
findcidade3
undefined
the soluction for my problem
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)
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
i gonna ty its gonna be like this 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 = "Some error"; next(); , (req, res) => if(req.error) res.status(400).send(req.error); else res.status(200).send(req.body.result); );
– Rsrd
Aug 22 at 16:42