Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DeprecationWarning: useNewUrlParser: true #6667

Closed
marianagarciaferreira opened this issue Jul 4, 2018 · 30 comments
Closed

DeprecationWarning: useNewUrlParser: true #6667

marianagarciaferreira opened this issue Jul 4, 2018 · 30 comments

Comments

@marianagarciaferreira
Copy link

warning in debug

DeprecationWarning: current URL string parser is deprecated, and will be removed in a future version. To use the new parser, pass option { useNewUrlParser: true } to MongoClient.connect.
Does anyone know how to fix this alert?

@lineus
Copy link
Collaborator

lineus commented Jul 4, 2018

see #6656, #6648, #6647, #6651 for more info @marianagarciaferreira.

How you handle this essentially boils down to how complex your connection is, and whether or not you are willing to silence deprecation warnings while using mongoose 5.2.1.

If you're connecting to a single server like localhost, just be sure to specify the port in your URI and add useNewUriParser: true to your connection options.

If you're connecting to a hosted service like Atlas or any kind of replica set, etc then you can either continue to use the old url parser ( you can temporarily silence the deprecation warnings with node --no-deprecation ) or you can remove the offending options from the uri string and put them in the options object as demonstrated in the issues above.

@adamreisnz
Copy link
Contributor

adamreisnz commented Jul 26, 2018

@lineus I am passing useNewUrlParser: true to my connect, yet it still complains about this deprecation warning:

const config = {
  autoIndex: false,
  useNewUrlParser: true,
};
return mongoose.connect(uri, {config});

Result:

(node:66058) DeprecationWarning: current URL string parser is deprecated, and will be removed in a future version. To use the new parser, pass option { useNewUrlParser: true } to MongoClient.connect.

It appears as if Mongoose is not passing it along to the Mongo client properly, any thoughts?
Mongoose version 5.2.5.

@lineus
Copy link
Collaborator

lineus commented Jul 26, 2018

@adamreisnz are you requiring/importing any modules (plugins etc) that also connect to the db?

@adamreisnz
Copy link
Contributor

adamreisnz commented Jul 26, 2018 via email

@lineus
Copy link
Collaborator

lineus commented Jul 26, 2018

mongoose.connect(uri, {config}); <- the curly braces around config mean you're passing an object with a property called config that contains the settings. Maybe try return mongoose.connect(uri, config); instead?

@adamreisnz
Copy link
Contributor

Doh, you are right, easy to miss that... >.<

Now it looks like it's trying to use the new parser, but telling me "port must be specified", for connection string mongodb://localhost:27017/db-name. Any thoughts?

@lineus
Copy link
Collaborator

lineus commented Jul 26, 2018

yeah, the newUrlParser doesn't allow the implicit default port, there's a native driver bug for it. For now you have to specify the port with the new url parser.

@marianagarciaferreira
Copy link
Author

I continue with the error

my options

.ENV
MONGO_URI=mongodb://localhost:27017/webservicebd

My Vars

mongo: {
dbUrl: process.env.WEBSERVICE_MONGO_URL,
uri: process.env.NODE_ENV === 'test'
? process.env.MONGO_URI_TESTS
: process.env.WEBSERVICE_MONGO_URL,
opt: {
keepAlive: 1,
reconnectTries: 30,
useNewUrlParser: true,
user: process.env.MONGO_USER,
pass: process.env.MONGO_PASSWORD,
auth: {
authdb: process.env.MONGO_DB
}'
}
}

here connect

exports.connect = () => {
mongoose.connect(mongo.uri, mongo.opt).then(
() => { return mongoose.connection },
err => { console.log(err) }
)
}

even though the error appears on the console.
I use mongoDB4 and "mongoose": "^ 5.2.1",

(node:1984) DeprecationWarning: current URL string parser is deprecated, and will be removed in a future version. To use the new parser, pass option { useNewUrlParser: true } to MongoClient.connect.

@adamreisnz
Copy link
Contributor

adamreisnz commented Jul 26, 2018 via email

@lineus
Copy link
Collaborator

lineus commented Jul 26, 2018

@marianagarciaferreira the first thing that stands out from your code sample is that your .env doesn't show a property that lines up with either side of this ternary ( but that doesn't explain your error ):

uri: process.env.NODE_ENV === 'test'
? process.env.MONGO_URI_TESTS // isn't defined in your code sample
: process.env.WEBSERVICE_MONGO_URL, // isn't defined in your code sample

I'll play around with your code and see if I can suss out what's going wrong.

@adamreisnz My bad, I didn't catch that. If you're not including any other packages that call .connect() or calling connect somewhere else in your code then I'm not sure what's going on.

This stand alone example works without error:

test.js

#!/usr/bin/env node
'use strict';

const mongoose = require('mongoose');
mongoose.connect('mongodb://localhost:27017/test', { useNewUrlParser: true });
const conn = mongoose.connection;
const Schema = mongoose.Schema;

const schema = new Schema({
  name: String
});

const Test = mongoose.model('test', schema);

const test = new Test({ name: 'one' });

async function run() {
  console.log(`Mongoose: ${mongoose.version}`);
  await conn.dropDatabase();
  await test.save();
  let found = await Test.findOne();
  console.log(found);
  return conn.close();
}

run();

Output:

$: ./test.js
Mongoose: 5.2.5
{ _id: 5b59bc70a663fabd73bdb8ed, name: 'one', __v: 0 }
$:

Can you craft an example that shows the error when passing in the port in the connection string?

@lineus
Copy link
Collaborator

lineus commented Jul 26, 2018

@marianagarciaferreira I tried to replicate what you're seeing in a complete example but it works for me.

The one mistake I made while making this example was that I instinctively called .connect() with mongo.opts instead of mongo.opt.

index.js

#!/usr/bin/env node
'use strict';

const db = require('./conn');

async function run() {
  const conn = await db.connect();
  const Test = conn.model('test', { name: String });
  const test = new Test({ name: 'test' });
  await conn.dropDatabase();
  await test.save();
  let found = await Test.findOne({});
  console.log(found);
  return conn.close();
}

run().catch(console.error);

conn.js

'use strict';

const mongoose = require('mongoose');
console.log(`Mongoose: ${mongoose.version}`);
const mongo = {
  uri: 'mongodb://localhost:27017/test',
  opt: {
    useNewUrlParser: true
  }
};

exports.connect = () => {
  return mongoose.connect(mongo.uri, mongo.opt)
    .then(() => { return mongoose.connection; });
};

Output:

marian: ./index.js
Mongoose: 5.2.1
{ _id: 5b59c634bd1a97c11df140d6, name: 'test', __v: 0 }
marian:

@adamreisnz
Copy link
Contributor

@lineus I've isolated the issue, it was a config being overwritten on our end, so with the port it is in fact working correctly. I will follow the other issue to stay updated on when the default port works again, thanks!

@pSnehanshu
Copy link

pSnehanshu commented Aug 17, 2018

Passing newUrlParser:true option to mongoose.connect causes the following issue when trying to do model.findOne:

{ MongoError: user is not allowed to do action [find] on [dbName.CollectionName]
at queryCallback .............
..............................
..............................
at _combinedTickCallback (internal/process/next_tick.js:131:7)
at process._tickCallback (internal/process/next_tick.js:180:9)
name: 'MongoError',
message: 'user is not allowed to do action [find] on [dbName.CollectionName]',
ok: 0,
errmsg: 'user is not allowed to do action [find] on [dbName.CollectionName]',
code: 8000,
codeName: 'AtlasError',
[Symbol(mongoErrorContextSymbol)]: { } 
}

Removing the options stops the error, however it starts showing the DeprecationWarning warning.
(node:4185) DeprecationWarning: current URL string parser is deprecated, and will be removed in a future version. To use the new parser, pass option { useNewUrlParser: true } to MongoClient.connect.
And yes, I'm using MongoDB Atlas.

@lineus
Copy link
Collaborator

lineus commented Aug 17, 2018

@pSnehanshu what version of mongoose are you using? what does your connection configuration look like? please share an example of how you're connecting to mongoose with any sensitive information redacted.

@pSnehanshu
Copy link

@lineus This is how I'm trying to connect:

mongoose.connect( 'mongodb+srv://clusterxx-xxxx.mongodb.net/?retryWrites=true' , {
     user: "user1",
     pass: "xxxxxxxx",
     dbName: "db1",
     newUrlParser: true,
})

@lineus
Copy link
Collaborator

lineus commented Aug 17, 2018

@pSnehanshu what version of mongoose are you using?

@pSnehanshu
Copy link

pSnehanshu commented Aug 17, 2018

@lineus "mongoose": "5.2.8"

@lineus
Copy link
Collaborator

lineus commented Aug 17, 2018

@pSnehanshu if possible can you update to @latest and try again. There were issues with the underlying driver in mongoose 5.2.0. The latest fixes for the driver are included in 5.2.9.

@pSnehanshu
Copy link

@lineus No, upgrading to v5.2.9 didn't help, instead it is spitting out even more warnings:
(node:10520) DeprecationWarning: collection.ensureIndex is deprecated. Use createIndexes instead.
and
(node:10520) DeprecationWarning: collection.find option [fields] is deprecated and will be removed in a later version.
Btw, what does this one means? Why will find be removed in the future?

@pSnehanshu
Copy link

I have also posted the issue in MongoDB Jira.

@lineus
Copy link
Collaborator

lineus commented Aug 17, 2018

@pSnehanshu when I suggested that you upgrade I thought you were using 5.2.0, and I was unaware of the new deprecation warnings in 5.2.9. Now that I know you're using above 5.2.0 I'll try and replicate the issue with you. It might be easier if you joined either Gitter.im or Slack to chatabout it in real time.

@pSnehanshu
Copy link

@lineus I am on gitter

@alanosman
Copy link

Hi @lineus I am having the same issue as @pSnehanshu after upgrading mongoose. I am seeing the DeprecationWarning: collection.find option issue and not sure how to resolve that. Any ideas?

@lineus
Copy link
Collaborator

lineus commented Aug 18, 2018

@alanosman this is something that mongoose will have to deal with eventually, by a passing projections property instead of a fields property in the query options.

For now, you'll have to ignore this specific deprecation warning. This is a result of mongoose updating to the latest version of the mongodb native js driver. They have added deprecation warnings for several methods and the option you mentioned.

You can see the details in the diff here

There's also a bug opened in this queue for this issue that you can follow.

@andersonaguiar
Copy link

Same issue here

@jeppy
Copy link

jeppy commented Aug 19, 2018

+1

@LeeviHalme
Copy link

Same issue with deprecated .find() method

@koolamusic
Copy link

I am working with Mongoose 5.2.10
this method works for me when connecting to a local database

const mongoose = require('mongoose');

mongoose.connect('mongodb://localhost/testdb', {useNewUrlParser: true})
.then(() => console.log('connecting to database successful'))
.catch(err => console.error('could not connect to mongo DB', err))

@apyrkh
Copy link

apyrkh commented Sep 18, 2018

still does not work

mongoose.connect('mongodb+srv://cluster0-xxxxx.mongodb.net', {
  "user": "user",
  "pass": "pass",
  "dbName": "prod",
  "keepAlive": 1,
  "useNewUrlParser": true
})

MongoError occurs when executes collection.findById. With useNewUrlParser: false everything is OK.

MongoError: user is not allowed to do action [find] on [prod.collection]
    at queryCallback (<path_to_project>\node_modules\mongoose\node_modules\mongodb-core\lib\cursor.js:248:25)
    at path_to_project\node_modules\mongoose\node_modules\mongodb-core\lib\connection\pool.js:532:18
    at process._tickCallback (internal/process/next_tick.js:61:11)

@lineus
Copy link
Collaborator

lineus commented Sep 19, 2018

@apyrkh this is a known issue. For now, when using the srv record, you need to provide your user/pass in the connection string to use the new URL parser. Alternatively, you can skip the new URL parser and live with the deprecation warnings.

The latest version of the native driver fixes this issue. The updated driver will land in mongoose soon.

@Automattic Automattic locked as resolved and limited conversation to collaborators Sep 25, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

10 participants