Managing various parameters within express routes

Currently, I am in the process of developing a website that utilizes Express and routing to manage HTTP requests.

As part of this project, I am dynamically populating my HTML div elements using Handlebars:

<div class="popup center" style="height: 15em;">
    <h3 name="name" id="name">{{ active_name }}</h3>
    <p name="description" id='description'>{{ active_desc }}</p>
</div>

Users are able to modify these elements using the contenteditable="true" attribute.

My goal is to create a button that, when clicked, will call a route and pass all the fields that have been modified.

Currently, I have only been able to pass one element ({{session.recipe}} which represents the recipe's ID), but I also want to handle the modified name and description fields.

<a href="/save-recipe/{{session.recipe}}" style="color: black;">
  <button name="button" class="greyishBackground width45 right">
      <p>Save</p>
  </button>
</a>

In my index.js file, I have the following code snippet:

router.get('/save-recipe/:id/', function(req, res, next) {
    var recipe_id = req.params.id; 

    console.log(req.body.name); // does not work - returns undefined
}

Since the code snippet req.body.name is not working (returning undefined), I am exploring ways to pass additional parameters to the route and handle them similarly to how I handled the recipe_id parameter (req.params.id).

Can someone provide guidance on how to achieve this? Is it possible to create an object with name and description fields and pass only the object? How would I go about doing this?

Alternatively, is there a way to make the req.body.name snippet work?

Answer №1

router.get('/store-recipe/:id/:title/:description', function(req, res, next) {
var recipe_id = req.params.id; 
var recipe_title = req.params.title;
var recipe_description = req.params.description;
}

req.body.title is not defined because no data is being sent to the server.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Implementing one-to-many relationships using Knex.js and Bookshelf.js in an ExpressJS/Postgres environment

When a user registers, I want to create a record on two tables simultaneously. user.js const db = require('../database'); const User = db.Model.extend({ tableName: 'login_user', hasSecurePassword: true, hasTimestamps: true, t ...

Javascript Select Element with No Options

I'm currently working on a <select> element that is being populated from an array using the following code snippet: <select id="selector" name="selector"</select> <button type=button onclick=populateSelect(states)>Click me1!</ ...

Challenges with Validating Bootstrap Datepicker Functionality

I need to restrict the datepicker input control to only allow selection of dates from the current date onwards. Despite trying to implement validation using the bootstrap-datepicker library and the following JavaScript code, I am still facing issues: $(& ...

Steps for interacting with a button of the <input> tag in Selenium using Python

As I attempt to complete a form submission, I encounter an issue where clicking the submit button does not produce any action. It seems that the problem lies with the button being tagged as <input>: <input type="submit" name="submit ...

v-treeview component triggering method execution twice upon input

I'm facing an issue with my Vue component that contains a treeview. Upon selecting a node, the goal is to update an array and display the checkbox as selected. However, I'm encountering a problem where if I select elements from one folder in the ...

Guide on making an NPM package with a web worker integration

Currently, I am in the process of developing an npm package that incorporates a web worker with type module. The issue arises when I try to link this package to a React application for testing purposes since the application cannot locate the worker.js file ...

Issues with jQuery compatibility when used alongside HTML and CSS

My coding tool is flagging errors in my JavaScript file, including: -'$' usage before definition. -Incorrect spacing between 'function' and '(' This is the content of my JS file: $(document).ready(function() { $(window).s ...

What steps should I take to handle and utilize a JSON request that is expected to be improperly structured?

My current project involves setting up a system where I receive a POST request that then triggers another POST request. The issue is that the incoming request has a Content-Type of application/json, but the body data appears to be incorrectly formatted JSO ...

How can I automatically center a Vimeo iframe vertically without having to manually adjust the position?

I'm trying to adjust a popular fluid jQuery script so that it can automatically center a vimeo video vertically, without any black bars. A little horizontal cropping is acceptable. Here is my current code: HTML <div class="container"> < ...

The React.js .map function encountered an error while trying to map the results from Firebase

As a newcomer to the realm of React and Firebase, I find myself struggling with arrays and objects. It seems like the way my data is formatted or typed does not play well with the .map method. Despite scouring Stack Overflow for answers, none of the soluti ...

There are two identical instances of the function and class named "Ajax" within the prototype

Within the current project I am involved in, we have incorporated and utilized a function called Ajax repeatedly throughout various sections. function Ajax (recvType, waitId) I am considering implementing the "prototype framework" for the Ajax class. Sh ...

Searching in React can be done dynamically by referencing an array, similar to how the "LIKE" function works in SQL

Currently, I'm tackling an issue with a dropdown feature. My goal is to have the dropdown results sorted when the user enters something into the input field. The values in the dropdown are sourced from an array. For instance, codeList = [N1, N2, N3, ...

Is there an undocumented property lurking in the depths of the Webpack server

The document specifies that Options that work with webpack-dev-middleware will have a key symbol next to them. I couldn't find any information on "cookieDomainRewrite" but when I tried using it, it worked. Any insights? Or suggestions on how I ...

Recommendation: 3 options for radio buttons on the registration form

My form includes a section where users need to choose if they want to sign up for a session that occurs 3 times daily. The catch is, only 5 applicants can enroll in each session (AM, Mid-day, PM). It's a competition to secure a spot. Here is the form ...

Is there a way to verify the identity of two fields using an external script such as "signup.js"?

My current project involves working with Electron, and it consists of three essential files. The first file is index.html: <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <link rel="styles ...

Navigating a Frame and Clicking a Link with Puppeteer: A Step-by-Step Guide

I am facing an issue with clicking on an anchor link within a page that is supposed to open a new tab for exporting a PDF. The problem arises because this link is located inside a frame within a frameset structure as shown below: https://i.stack.imgur.com ...

The Express App showcasing the classic "Hello World" message is serving up binary information

Hey there, I'm facing an issue with this simple Express sample code. All I want is to get the basic '/' home page to work, but it's not cooperating. Here's the code snippet: var express = require('express'); var sR = requ ...

There seems to be a bit of uncertainty surrounding the concept of

app.get("/", (req, res, next) => { next(); }); app.get("/admin", (req, res, next) => { res.send("<h1>Admin page</h1>"); }); app.listen(3000); console.log("Listening on port 3000"); There seem ...

Calculate averages of an array and show them when the page loads using Vue

In my coding project, there is an array named products that follows this specific structure: { "_id": "150", "name": "Milk", "description": "Skimmed", "price": "10", "ratings": [ ...

Troubleshooting: JavaScript Bookmarklet Fails to Execute on Certain Websites

Recently, I created a unique bookmarklet that functions flawlessly on some websites, but unfortunately fails to work on others. Interestingly, even when it doesn't work, the script is still added to the bottom of the page; however, only a portion of t ...