Home > Enterprise >  The "url" argument must be of type string. Received undefined
The "url" argument must be of type string. Received undefined

Time:06-01

I am trying to deploy my custom express/node app to digitalocean app platform. But recieving this error during the deployment:

deployment error

This is my package.json

{
  "name": "backend",
  "version": "1.0.0",
  "main": "app.js",
  "license": "MIT",
  "dependencies": {
    "bcrypt": "^5.0.1",
    "cors": "^2.8.5",
    "dotenv": "^16.0.1",
    "express": "^4.18.1",
    "jsonwebtoken": "^8.5.1",
    "mysql2": "^2.3.3",
    "nodemailer": "^6.7.5",
    "nodemon": "^2.0.16",
    "passport": "^0.5.2",
    "passport-jwt": "^4.0.0",
    "passport-local": "^1.0.0",
    "pg": "^8.7.3",
    "pg-hstore": "^2.3.4",
    "sequelize": "^6.19.0",
    "sequelize-cli": "^6.4.1"
  },
  "scripts": {
    "start": "nodemon app.js",
    "deploy": "node app.js"
  },
  "engines": {
    "node": "16.15"
  }
}

This is the config.json in db folder:

    {
  "development": {
    "username": "postgres",
    "password": "10300",
    "database": "weblogin",
    "host": "127.0.0.1",
    "dialect": "postgres",
    "logging": false
  },
  "test": {
    "username": "root",
    "password": null,
    "database": "database_test",
    "host": "127.0.0.1",
    "dialect": "mysql"
  },
  "production": {
    "use_env_variable": "DATABASE_URL"
  }
}

Also added this segment in models/index.js

  ...
sequelize = new Sequelize(process.env[config.use_env_variable], {
    dialect: "postgres",
    dialectOptions: {
      ssl: true,
    },
  });
} else {
...

UPDATE when I run the vscode debugger it shows same error preventing it to send any variables. debugger

I don't know from where the error is coming from.

This is app.js

const express = require("express");
const app = express();
const db = require("./db/models");
const cors = require("cors");
const passport = require("passport");
const { localStrategy, jwtStrategy } = require("./middleware/passport");
const userRoutes = require("./routes/users");
require("dotenv").config();

//middleware
app.use(express.json());
app.use(cors());

//passport
app.use(passport.initialize());
passport.use(localStrategy);
passport.use(jwtStrategy);

//routes
app.use(userRoutes);

//Not Found
app.use((req, res, next) => {
  next({
    status: 404,
    message: "Path not found",
  });
});

//Error Handling
app.use((error, req, res, next) => {
  res
    .status(error.status || 500)
    .json({ message: error.message || "Internal Server Error" });
});

db.sequelize.sync({ alter: true });
//db.sequelize.sync({ force: true });

const PORT = process.env.PORT || 8080;

app.listen(PORT, () => {
  console.log("application is running"   PORT);
});

Can you help me figure how to solve this issue? Thanks.

CodePudding user response:

Could you please add the actual code which throws the error?

Even better would be the debugged state, you can either attach a debguger in VS Code or just type debugger; in the code and then start it from the CLI and then show the code and the values.

We can't help if we don't know what is actually causing the problem

CodePudding user response:

Reading tracebacks is a pain in xss neck that you must learn to do. When you do, you'll see that your line containing new Sequelize called other functions that eventually caused your crash. Use file and line numbers to figure out which of your lines of code is responsible. The crash itself is due to the fact that the crashing code expected some variable named url to be a string, but instead found that it was undefined.

Once you've identified the line in your code that eventually led to the crash, you then examine that line. It is, I believe, this one.

sequelize = new Sequelize(process.env[config.use_env_variable],

Ordinarily the first parameter to that Sequelize constructor is the URL of a dbms server. But your program is complaining of a null URL.

So, you may wish to change your code to:

const sequelizeURL = process.env[config.use_env_variable]
console.log(sequelizeURL)
sequelize = new Sequelize(sequelizeURL,

I suspect you will discover the first of those three lines assigns a null value to sequelizeURL. You certainly will find that your call to the Sequelize constructor has something wrong in it.

It's easy to handle config and environment variables incorrectly. And when you deploy on Heroku, you must handle them correctly or, boom!

  • Related