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
2 Answers
Reset to default 2your 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
版权声明:本文标题:javascript - ERR_INVALID_ARG_TYPE error - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741251408a2365860.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论