Skip to main content
GameDev.net gamedev.net
🔒 Locked

can´t make your two or more methods in same route

Started by Pedro Alves Aug 22, 2018 at 1:36 AM 1 replies 5k views
Original Post
Pedro Alves
Pedro Alves

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

what i doing wrong


my code


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


Hello
Avalander
Avalander

Alright, you'll have to help me a bit here, because I don't get your code.

  1. First and foremost, why do you want to have two or more handlers for the same route? You can only send one response for each HTTP request, so it doesn't make sense to have more than one function handle the request.
  2. Where are you trying to put more than one handlers for the same route? The only routes I see in your code are different.
    
    router.get('/Administration/Alliances', (req, res) => controllerAdmin.findcidade3(connection, req, res))
    router.get('/Administration/Alliances/limitado', (req, res) => controlleruser.findAlianca(connection, req, res))


  3. Which endpoint are you calling when you get the error?
  4. Does any error or stack trace appear in your console?

Also, the way you pasted the code is really hard to follow, next time can you put each file in a different code snippet with the name of the file at the top?

Here's an example of what I mean:


// main.js
const { twilight } = require('ponies')

console.log(twilight)

// ponies.js
module.exports = {
  twilight: { name: 'Twilight Sparkle', type: 'alicorn', element: 'magic' },
  rainbow: { name: 'Rainbow Dash', type: 'pegasus', element: 'loyalty' },
  pinkie: { name: 'Pinkie Pie', type: 'earth pony', element: 'laughter' },
  rarity: { name: 'Rarity', type: 'unicorn', element: 'generosity' },
  fluttershy: { name: 'Fluttershy', type: 'pegasus', element: 'kindness' },
  applejack: { name: 'Applejack', type: 'earth pony', element: 'honesty' },
}

And make sure that your code is properly formatted, there are many tools out there to format your code nicely for you. With these two things, it would be a lot easier to understand your code and try to help you.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.