Error message: Cannot establish the attribute 'next' on the string '/:id'

I encountered an error while trying to develop an API for a travel company. The error message "TypeError: Cannot create property 'next' on string '/:id'" keeps popping up, even though all the necessary functions are already created.

const express = require('express');
const port = 3000
const app = express();
const tourRouter = express.Router();
tourRouter.route('/').
    get(getAllTours)
    .post(createTour);
tourRouter('/:id').
    get(getTour)
    .patch(updateTour)
    .delete(deleteTour);
app.use('/api/v1/tours', tourRouter)
app.listen(port)

The specific error is:

 req.next = next;           ^
TypeError: Cannot create property 'next' on string '/:id'
    at Function.handle (C:\Users\dell\OneDrive\Desktop\node\starter\node_modules\express\lib\router\index.js:160:12)
    at router 

Answer №1

Looks like you missed out on .route in this section:

tourRouter('/:id').

The correct format should be

tourRouter.route('/:id').

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

javascript onload select the text in the textarea

Is there a way to have JavaScript automatically highlight the text inside a textarea when the page is loaded? ...

JS call for Bootstrap 5 collapse toggle not functioning as expected

I'm exploring the use of JavaScript to display or hide a collapsible element without toggling between the two states. In other words, I want to prevent the toggle functionality. According to the information provided in the documentation at https://ge ...

Trouble concealing tab using slideUp function in Jquery

Whenever I click on the 'a' tag, it displays additional HTML content (list) that is controlled by generic JS code for tabs. However, I want to hide the list when I click on the "Title" link again. What can I do to achieve this? You can view a de ...

Steps for installing npm packages on Plesk

I attempted to incorporate node-apk-parserhttps://www.npmjs.com/package/node-apk-parser into my Node.js project. It successfully installs when I use npm install in my VS package manager console. However, after updating the package.json on Plesk and click ...

Next.js API Endpoint Call Resulting in Empty Object Returned by Fetch Function

Having an issue with making an API call in Next.js to delete an item from the database. I'm using the "body" field of the fetch to send a string to the API. The fetch call is within a Next.JS page, and the API endpoint is located in the API folder gen ...

What are the best ways to establish communication among JavaScript modules?

I have multiple modules in my application, each with their own responsibilities, but I'm unclear on how they should communicate with one another. What is the best way for modules to interact with each other? How can modules notify or listen to e ...

Bringing in d3js into Angular 2

Is there a way to successfully import d3js into an Angular2 project? I have already installed d3js using npm and added it to my systemJs, but am encountering a traceur.js error. I also attempted to just use the latest cdn in a script tag and tried import * ...

When provided with varied inputs, new Date() yields distinct values for various time zones

var date1 = "2015-03-29"; console.log(new Date(date1)); //Output:Sun Mar 29 2015 05:30:00 GMT+0530 (India Standard Time) var date2 = "1869-12-31"; console.log(new Date(date2)); //Output:Fri Dec 31 1869 05:53:20 GMT+0553 (India Standard ...

Having difficulty displaying a partial view within a view while making an AJAX call

Trying to figure out what's missing in my code. I have a view with some radio buttons and I want to display a different partial view when a radio button is selected. Here's the snippet of my code: Controller public ActionResult Method(string va ...

Comparison between the curl command and Node.js's request functionality

Is there a way to correctly execute this cURL shell command curl --data "{\"obj\" : \"1234556\"}" --digest "https://USERNAME:<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="5505140606021a0711152222227b263a38" ...

What is the best way to design a timetable schema in MongoDB specifically for a Teacher Schema?

Greetings to all in the StackOverflow community! I am here seeking some innovative ideas for a project I am currently working on. One of the challenges I'm facing involves storing available days within a Teacher Schema. In this application, a Teacher ...

Having trouble installing Cordova Ionic through npm

Is there a solution to resolve this issue? C:\WINDOWS\system32>npm install -g cordova ionic npm ERR! code E404 npm ERR! 404 Not Found: pinkie-promise@^2.0.0 npm ERR! For more details, check the following log: npm ERR! C:\Users&bs ...

Electron / Atom Shell unable to locate specified module

I am completely new to npm, node, and Electron. Here is the structure of my folder: -package.json -index.html -main.js -js/myStuff.js -node_modules In the file myStuff.js, I have a line that says var chokidar = require('chokidar'); but it keep ...

Extracting the JQuery library from its source code

Is there a simple method for extracting only the necessary functions from the jQuery library? I have found that many of the functions within the library are not being utilized, so it would be beneficial to remove them. ...

Vue CLI Plugin Electron Builder displays a completely empty screen after compiling

After building my electron app using this specific plugin, I encountered a frustrating issue where the installed package would only display a blank, white screen. Despite setting up the window to open dev tools in the built version, inspecting the page rev ...

Is there a way to ensure a Javascript alert appears just one time and never again?

I struggle with Javascript, but I have a specific task that needs to be completed. I am participating in a school competition and need an alert to appear only once throughout the entire project, not just once per browsing session. The alert will inform ...

Using CSS, center a div element both vertically and horizontally, and position the footer at the bottom of the page

I am encountering some unexpected behavior for a task that I thought would be simple. I have two main objectives in mind. Firstly, I want the footer to be displayed at the bottom of the page as an actual footer. Secondly, I need the div with the ".center-d ...

Resizing Div elements in React

I am currently working on a Drawer navigation feature and I am exploring the option of enabling mouse drag resize functionality. To achieve this, I have included a div element where I listen for the onMouseDown event. Once triggered, I then add an event li ...

Update JSON data in ng-blur event using AngularJS

Could you offer some guidance on how to push the content from a text box into JSON when I click outside of the box? Below is the code for my text box: <input type="text" name="treatmentCost" class="form-control" ng-model="addTemplate" /> And here i ...

Tips for organizing JSON data from Google Maps' Direction Service for storage in a MongoDB database and displaying it on a map

Utilizing the Google Maps Directions service for navigation, I aim to preserve the response in MongoDB in JSON format. This will allow me to easily plot the route on the map again. How should I structure the JSON data for the routes array retrieved from th ...