Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

83
Visualizações
req.params changes between calls

I am writing a small node.js app on glitch.me and I have run into problem I don't really understand. It's kind of a message board (a project for freeCodeCamp), where you can post a thread (specifiyng a board) and then see the list of messages on the board. Threads are stored in mongodb collection. The name of the board is accessed through req.params.board. The problem is, req.params.board somehow gets 1 symbol shorter between two function calls. Here are my routing file and my handlers (in a separate module), and in handlers there are console.logs that will show you what I mean. Let's say we create a post on 'newBoard' board:

api.js

    module.exports = function (app) {

  app.route('/api/threads/:board')

  .get(threadHandler.getThreads)
  .post(threadHandler.postThread)
  .delete(threadHandler.deleteThread)
  .put(threadHandler.reportThread)

  app.route('/api/replies/:board')
  .post(threadHandler.postReply)
  .get(threadHandler.getReplies)
  .delete(threadHandler.deleteReply)
  .put(threadHandler.reportReply)

};

handlers:

  module.exports.postThread = function(req, res){
  console.log(req.params.board); //logs newBoard
  var newThread = new Thread({
    text: req.body.text,
    delete_password: req.body.delete_password,
    created_on: new Date(),
    bumped_on: new Date(),
    reported: false,
    replies: [],
    replycount: 0,
    board: req.params.board
  })
  newThread.save();
  res.redirect('/b/'+req.params.board);  
}

module.exports.getThreads = function(req, res){
//gets 10 last messages on the board
  console.log(req.params.board); //logs newBoar
  Thread.find({}).sort({bumped_on: -1}).limit(10).exec(function (err, docs){
    if (err) return;
    docs.forEach(function(doc)
     {
          if(doc.replies.length > 3) {
          doc.replies = doc.replies.slice(-3);
          }
          })
    res.json(docs);
  })
}
 module.exports.getReplies = function(req, res){
 //gets a separate thread with all the replies
  console.log(req.params.board, 'reply'); //logs newBoard + _id of the  thread, so the url is '/newBoard/5900d2da926ef6277e143564' it will log '/newBoard5900d2da926ef6277e143564', 'eating' the slash between board and id.

  Thread.findById(req.query.thread_id, function(err, foundThread){
    if (err) return;
    if (foundThread){
      res.json(foundThread);
    }
  })
}

I don't understand what's happening, so I'd be really grateful if somebody told me why does it happen and how to fix it. My whole project is here: https://glitch.com/edit/#!/shrouded-consonant

about 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

You are redirecting to /b/newBoard, but your HTML does this:

var currentBoard = window.location.pathname.slice(3,-1);

It assumes that the redirect is done to /b/newBoard/; with the -1, it's trying to slice the trailing slash, which doesn't exist. Hence newBoar.

The easiest solution is to fix the redirect:

res.redirect('/b/' + req.params.board + '/');
about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda