I'm having issues getting data from an API while learning Angular 2. This is the error I'm getting
url.js:106 throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'url', 'string', url);
TypeError [ERR_INVALID_ARG_TYPE]: The url argument must be of type string. Received type undefined at Url.parse (url.js:106:11)
This is the code I'm working with
index.js
const express = require('express');
const app = express();
const MongoClient = require('mongodb').MongoClient;
require('dotenv').config();
let database;
MongoClient.connect(process.env.DB_CONN, (err, db) => {
console.log('connected to mongodb...');
app.listen(3000, () => {
database = db;
console.log('listening on port 3000...')
});
});
app.get('/contacts', (req, res) => {
const contactsCollection = database.collection('contacts');
contactsCollection.find({}).toArray((err, docs) => {
if(err) {
console.log(err);
}
return res.json(docs);
});
});
.env
DB_CONN=mongodb://admin:[email protected]:17931/dem-contact
I do not understand the error and the only url I have is in my env file since I'm using mlab mongodb. What's wrong with my code?