admin管理员组

文章数量:1277565

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?

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?

Share Improve this question edited Apr 18, 2018 at 7:47 Mena asked Apr 18, 2018 at 7:39 MenaMena 2,0297 gold badges42 silver badges90 bronze badges 5
  • Could you provide a sample of your .env file please? – Joseph Reeve Commented Apr 18, 2018 at 7:44
  • @JosephReeve added content of .env file in my edit – Mena Commented Apr 18, 2018 at 7:48
  • I understand that your env var process.env.DB_CONN is not being processed correctly, it's being passed as undefined. How are you launching the app? – daniegarcia254 Commented Apr 18, 2018 at 7:49
  • how are you running your app ? through some IDE's console ? – Muhammad Usman Commented Apr 18, 2018 at 8:42
  • Just wanted to add, this can error can also be caused because of you are writing the wrong variable name. – Irfandy J. Commented Sep 9, 2019 at 9:15
Add a ment  | 

2 Answers 2

Reset to default 2

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' })

Unrelated to the specific question, but for future readers:

The error ERR_INVALID_ARG_TYPE is thrown in other circumstances too. In my case, I was passing null to path.join().

本文标签: javascriptERRINVALIDARGTYPE errorStack Overflow