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
Deprecation Warning #4291
Comments
Hmm are you using promises anywhere? You shouldn't be getting this warning unless you use promises... |
nop I'm not using promises |
I've just started to get this issue too. Not using promises that I know of! |
I solved that warning doing
Right before calling mongoose.connect
|
Yeah this warning is supposed to tell you to change mongooses default promise lib if you're using promises, but it shouldn't happen unless you call .then somewhere |
I'm getting it but I'm only getting it where I save a object then modify it in the callback and save again |
I've tried the @saudelog approach to assign a promise to mongoose before mongoose connect but I still receiving this error I'm using async functions es7, and my code is blocked when I try to Model.findOne({}) |
The solution @saudelog provides works. Like @IrishAdo I'm only getting this when saving an object also. Perhaps it is because of one of the mongoose plugins I'm using using Bluebird promise library. Here is a list of dependencies I'm using
|
I'm not using plugins nor promises and getting this warning as well (on mongoose@4.5.3). Running my app with "--trace-deprecation" I've got:
|
I have the same issue, if it's any help it is specifically triggered when I try to create documents which have an array of subdocuments inside them, something like: var data = {
field: 'value',
another_field: [{subdocfield1: 'value', subdocfield2: 'value'}]
}
mySchema.create(data, cb) It does not occur with other collections that do not have subdocs, it doesn't even happen if I issue the create statement without the subdoc data, like this: var data = {
field: 'value'
}
mySchema.create(data, cb) Note: |
Experiencing same depreciation warning when I use |
I just got this error when using |
Getting this, glad to read it is not alarming. I shall keep a closer look into this to help debugging when it happens. |
Getting this as well, and I'm not using mongoose promises anywhere. I was able to trace it back to a |
Facing this issue on node v4.4.6 whereas on v4.4.2 this seems to work |
@skatiyar code sample please |
same with me, getting this message after invoking save() method, enough for me to care |
This works! Before |
The following code is enough to reproduce on my end: const mongoose = require('mongoose');
const schema = new mongoose.Schema({
data: { type: Number }
});
const Model = mongoose.model('schema', schema);
mongoose.connect('mongodb://127.0.0.1:27017/somedatabase');
const newModel = new Model({ data: 1 });
const save = newModel.save(() => {
console.log('saved model');
});
console.log(typeof save.then); //=> 'function' Strange thing I can see here is that using Here are my versions: Let me know if you need any other info from me. |
@dvlsg yes that's expected behavior currently, save will always return a promise even if you pass a callback |
Well, I guess that would at least explain why people are seeing the promise warning even when they don't think they're using promises. |
That explain everything.. I'm just a bit paranoid and wanted to get rid all of the warning 💃 |
It's my fault, yup .save() returns a promise so this is expected, but still, warning made me wonder. http://mongoosejs.com/docs/promises.html (just had to read first paragraph 🎱) |
as a fellow Russian speaker I apologize for the behavior this lesser being has displayed an urge you not to translate what it has written here. |
@vkarpov15 I have tried both Unfortunately, I am still receiving the deprecation warning. Is there something I am doing wrong? const mongoose = require('mongoose');
mongoose.Promise = global.Promise;
mongoose.connect('mongodb://localhost/users_test');
mongoose.connection
.once('open', () => {
console.log('connectecd to db')
})
.on('error', (err) => {
console.warn('Warning', err)
}); |
@protoEvangelium Are you importing mongoose elsewhere in your app? Is this the first time mongoose is being used or is this file imported into another file? Also what version of mongoose /node are you using? |
@varunjayaraman This file is not being imported anywhere else. However, I am importing mongoose in my user.js schema file and then using that User schema in some of my tests. const mongoose = require('mongoose')
const Schema = mongoose.Schema
const UserSchema = new Schema({
name: String,
})
const User = mongoose.model('user', UserSchema)
module.exports = User node v7.0.0 |
@protoEvangelium are you getting the warning in your app or only in your tests? |
I've had the same problem once - the culprit was another module, that used it's own mongoose dependency. Such modules have something like |
@saudelog Thanks for the answer |
@varunjayaraman I have only begun to test adding users to Mongo using Mongoose and Mocha. It is a barebones project so far. @Spown I am using:
Thanks :) |
@protoEvangelium wait a minute, did you try this? const mongoose = require('mongoose')
mongoose.Promise = global.Promise // <--
const Schema = mongoose.Schema
const UserSchema = new Schema({
name: String,
})
const User = mongoose.model('user', UserSchema)
module.exports = User if you create a model from a mongoose instance who's promise wasn't redefined - every query on this model would throw the warning. |
I happened across this thread at the right time. I was receiving the same warning anytime I was saving information. Turns out I was creating a model from an instance of mongoose and was not redefining it. Requiring bluebird and setting it to mongoose.Promise resolved the error message. Thanks so much! |
@Spown Thank you sir! That was the issue. Interestingly, I defined const mongoose = require('mongoose')
mongoose.Promise = global.Promise;
const Schema = mongoose.Schema |
I'm getting this issue when using "populatable" virtuals in my schemas. Running my app with
I have set the promise (note I'm using TypeScript):
Any ideas? Should I set the promise on every model that uses "populatable" virtuals? Hold on, I'm actually going to try that and come back with how it went. |
Okay, after some tinkering, I figured out that I still need to set the In my case, the |
Why not to use ES6 Promises? Aren't they available everywhere now? mongoose.Promise = Promise; // eh? |
Mocha tests were throwing an EventEmitter error fixed by setting require('events').EventEmitter.defaultMaxListerners = Infinity in app.js detailed here: npm/npm#13806. Mongoose Promises are apprently depricated, switch all uses of Mongoose to use NodeJS's global promises instead detailed here: Automattic/mongoose#4291 Changes to be committed: modified: app.js modified: test/mongoConnectTest.js modified: test/mongoStudentModelTest.js modified: test/mongoTutorModelTest.js modified: test/mongoUserModelTest.js
Thanks @Spown, I faced same deprecation warning since I put |
Declaring |
personally I |
yeah I do the same as @Spown. It's certainly not ideal, but it's the only way to keep mongoose backwards compatible until the next major release when we can deprecate mpromise completely |
1 - added "test" as a new key of "script". to run the tests type "npm test". 2 - added "chai", "chai-http", "mocha" modules in "devDependencies". * In dist.js (gulp task): 1 - Replaced clean() by clean({force: true}); * In tweets.routes.js: 1 - Replaced require(__dirname + '/tweets.controllers') by require('./tweets.controllers'); * In tweers.model.js: 1 - Fixed some schema paths. Replaced "require" by "required". 2 - exports the model. * In tweets.controllers.js: 1 - In function addNew: removed the manual check of the required params. Now required params are validated by the schema, remember that we fixed the "require" path with "required". * In mongoose.js: 1 - Descommented 'mongoose.Promise = global.Promise;'. Ref: Automattic/mongoose#4291
libs/mongoose.js import mongoose from 'mongoose'; // real mongoose only be required once here
mongoose.Promise = global.Promise; // so that you dont have to do this on every require
const Schema = mongoose.Schema;
export {
mongoose as default, // kind of singleton
mongoose,
Schema,
}; index.js import mongoose from './libs/mongoose';
mongoose.connect(process.env.MONGODB_URI); models/user.js import {mongoose, Schema} from '../libs/mongoose';
export default mongoose.model('User', new Schema({
name: String,
password: String,
})); |
@howard require() calls are cached. |
So, if I understand correctly, I need to set the promise before every schema declaration instead of doing it only in one place (for example in my index file or where I'm setting the connection) |
@sh-guti i think so, i had the same problem and that piece of code solve that. At this point i don't know, if to use callbacks or promises. LOL. |
Use
|
Can anyone explain, why on Earth mongoose denies the existence of native Promises and can't use them out of box? |
Backwards compatibility. Mongoose 4.x was around for over a year before node 4 was released. We'll switch over with the next backwards breaking release. |
When will this be? |
mongoose.createConnection(URI) causes a very strange bug with Mongoose , hours of debugging with queries not working . eventually went back to mongoose.connect(process.env.MONGODB); from mongoose.createConnection(process.env.MONGODB) as a side note , it did stop the depreciation warning .. but stopped everything working ... |
@MickL we will have an rc0 out in tbe next few weeks, follow mongoose on twitter or join the slack channel for updates |
I'm getting this warning
after I do
driver is an instance of Driver class
any thoughts about what Im doing wrong?
The text was updated successfully, but these errors were encountered: