Link to home
Create AccountLog in
Avatar of chalie001
chalie001

asked on

ReferenceError: userRoutes is not defined

hi am geting this error in my nodejs backend

[nodemon] restarting due to changes...
[nodemon] starting `node server.js`
C:\solution2\pagination-02-finished\backend\app.js:38
("/api/user", userRoutes);
^

ReferenceError: userRoutes is not defined
    at Object.<anonymous> (C:\solution2\pagination-02-finished\backend\app.js:38:1)
    at Module._compile (internal/modules/cjs/loader.js:778:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
    at Module.load (internal/modules/cjs/loader.js:653:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
    at Function.Module._load (internal/modules/cjs/loader.js:585:3)
    at Module.require (internal/modules/cjs/loader.js:692:17)
    at require (internal/modules/cjs/helpers.js:25:18)
    at Object.<anonymous> (C:\solution2\pagination-02-finished\server.js:1:13)
    at Module._compile (internal/modules/cjs/loader.js:778:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
    at Module.load (internal/modules/cjs/loader.js:653:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
    at Function.Module._load (internal/modules/cjs/loader.js:585:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:831:12)
    at startup (internal/bootstrap/node.js:283:19)
[nodemon] app crashed - waiting for file changes before starting...

const path = require("path");
const express = require("express");
const bodyParser = require("body-parser");
const mongoose = require("mongoose");

const postsRoutes = require("./routes/posts");

const app = express();

mongoose
  .connect("mongodb+srv://usename:pswd@mongodbcl-ws2rs.mongodb.net/dbname?retryWrites=true&w=majority"
  )
  .then(() => {
    console.log("Connected to database!");
  })
  .catch(() => {
    console.log("Connection failed!");
  });

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use("/images", express.static(path.join("backend/images")));

app.use((req, res, next) => {
  res.setHeader("Access-Control-Allow-Origin", "*");
  res.setHeader(
    "Access-Control-Allow-Headers",
    "Origin, X-Requested-With, Content-Type, Accept"
  );
  res.setHeader(
    "Access-Control-Allow-Methods",
    "GET, POST, PATCH, PUT, DELETE, OPTIONS"
  );
  next();
});

app.use("/api/posts", postsRoutes);
("/api/user", userRoutes);

module.exports = app;

Open in new window

Avatar of Louis LIETAER
Louis LIETAER
Flag of France image

line 38, I suppose it should be: app.use("/api/user", userRoutes);

or if not needed remove line
Avatar of chalie001
chalie001

ASKER

its needed am now geting [nodemon] starting `node server.js`
C:\solution2\pagination-02-finished\backend\app.js:38
app.use("/api/user", userRoutes);
                     ^

ReferenceError: userRoutes is not defined
    at Object.<anonymous> (C:\solution2\pagination-02-finished\backend\app.js:38:22)
    at Module._compile (internal/modules/cjs/loader.js:778:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
    at Module.load (internal/modules/cjs/loader.js:653:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
    at Function.Module._load (internal/modules/cjs/loader.js:585:3)
    at Module.require (internal/modules/cjs/loader.js:692:17)
    at require (internal/modules/cjs/helpers.js:25:18)
    at Object.<anonymous> (C:\solution2\pagination-02-finished\server.js:1:13)
    at Module._compile (internal/modules/cjs/loader.js:778:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
    at Module.load (internal/modules/cjs/loader.js:653:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
    at Function.Module._load (internal/modules/cjs/loader.js:585:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:831:12)
    at startup (internal/bootstrap/node.js:283:19)
[nodemon] app crashed - waiting for file changes before starting...
at beginning of your code :

const userRoutes = require("./routes/user.routes");

check if file ./routes/user.routes is existing in your path
this is not a file do you know nodejs ./routes/user.routes
User generated image
ASKER CERTIFIED SOLUTION
Avatar of Louis LIETAER
Louis LIETAER
Flag of France image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer