Friday, May 17, 2024
 Popular · Latest · Hot · Upcoming
163
rated 0 times [  168] [ 5]  / answers: 1 / hits: 38398  / 6 Years ago, wed, april 18, 2018, 12:00:00

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?


More From » angular

 Answers
1

your DB_CONN variable needs some quotes.



DB_CONN=mongodb://admin:[email protected]:17931/dem-contact
should be
DB_CONN='mongodb://admin:[email protected]:17931/dem-contact'



your .env file should be in the root of your project folder ( the same folder as the node_modules folder )



or you can pass the path property in the options to .config() like:



.config({ path: '/somewhere/else/.env' })


[#54634] Sunday, April 15, 2018, 6 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
theodore

Total Points: 318
Total Questions: 97
Total Answers: 119

Location: Turks and Caicos Islands
Member since Sun, Mar 7, 2021
3 Years ago
;