最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - (node:71307) [DEP0079] DeprecationWarning - Stack Overflow

programmeradmin2浏览0评论

Try to update MongoDB document Getting Deprecation Warning as

(node:71307) [DEP0079] DeprecationWarning: Custom inspection function on Objects via .inspect() is deprecated

Node version v10.5.0, db version v3.6.5, Mongoose version [email protected]

Campground.findById(campgroundId, function(err, campground){
    if(err){
        console.log(err);
    } else {
        console.log(campground.celebrity);
        Celebrity.create(celebrityData, function(err, celebrity){
            if(err){
                console.log(err);
            } else {
                //save comment
                celebrity.save();
                campground.celebrity.push(celebrity);
                campground.save();
                console.log(celebrity);
                //req.flash('success', 'Created a comment!');
            }
        });
    }
});

Try to update MongoDB document Getting Deprecation Warning as

(node:71307) [DEP0079] DeprecationWarning: Custom inspection function on Objects via .inspect() is deprecated

Node version v10.5.0, db version v3.6.5, Mongoose version [email protected]

Campground.findById(campgroundId, function(err, campground){
    if(err){
        console.log(err);
    } else {
        console.log(campground.celebrity);
        Celebrity.create(celebrityData, function(err, celebrity){
            if(err){
                console.log(err);
            } else {
                //save comment
                celebrity.save();
                campground.celebrity.push(celebrity);
                campground.save();
                console.log(celebrity);
                //req.flash('success', 'Created a comment!');
            }
        });
    }
});
Share Improve this question edited Jul 26, 2018 at 8:51 Kiran Ghatage asked Jul 20, 2018 at 6:10 Kiran GhatageKiran Ghatage 4071 gold badge8 silver badges15 bronze badges 4
  • 1 Please mention you node and mongoose version . – Himanshu sharma Commented Jul 20, 2018 at 6:17
  • mongoose version ? – Himanshu sharma Commented Jul 20, 2018 at 6:36
  • Mongoose version [email protected] – Kiran Ghatage Commented Jul 20, 2018 at 6:53
  • This waning will come in mongoose for now. they will do work on this . for now its safe to go with this. Nothing to worry. – Himanshu sharma Commented Jul 20, 2018 at 6:59
Add a comment  | 

9 Answers 9

Reset to default 14

You have to not worry about this error this is mongoose warning . Actually Mongoose use inspect() to debug output . they will update it may be before node 12.x . For now it is safe to use this.

Nothing to worry.

Check this info. https://nodejs.org/api/deprecations.html#deprecations_dep0079_custom_inspection_function_on_objects_via_inspect

DEP0079: Custom inspection function on Objects via .inspect()# Type: Runtime

Using a property named inspect on an object to specify a custom inspection function for util.inspect() is deprecated. Use util.inspect.custom instead. For backward compatibility with Node.js prior to version 6.4.0, both may be specified.

If you want more detail, see this . This is under progress. Warning will come in node 10

https://github.com/Automattic/mongoose/issues/6420

In order to not get the deprecation message you can upgrade to mongoose version 5.2.10 or later according to this Github mongoose issue and the set the following at a proper location in code:

mongoose.set('useCreateIndex', true)

upgrade to 5.2.10 and set

  mongoose.set('useCreateIndex', true);

Another way to set it is...

mongoose.connect(
    "mongodb://<user>:<password>@<url>",
    { 
      useNewUrlParser: true, 
      useCreateIndex: true 
    }
  )

More information can be found here: https://github.com/Automattic/mongoose/issues/6890

To solve this problem, You have to use useNewUrlParser and useCreateIndex. See the code below.

mongoose.connect(
 config.databaseUrl,
 { 
    useNewUrlParser: true, 
    useCreateIndex: true 
 }
)

Or

mongoose.set("useCreateIndex", true);    
mongoose.connect(
    config.databaseUrl,
    { 
        useNewUrlParser: true
    }
  );

You should add the options useCreateIndex to your connect method

mongoose.connect(keys.mongoURI, {
    useNewUrlParser: true,
    useCreateIndex: true,
})

Totally random but at least it's not a copycat answer: I was getting this deprecation warning (and unexpected behavior) when I accidentally used Model.find() instead of Model.findOne()

So my erroneous code looked like this

User.find(query)
.then(user => {
  console.log(user.emailSettings.confirmToken)
})

...with an ordinary object/array, this would fail with TypeError: Cannot read property 'confirmToken' of undefined but with an mongo document array apparently it does this inspecty thing and now gives this deprecation warning.

I want to add to this thread that it may also have to do with other dependencies.

For instance, nothing I updated or set for NodeJS, MongoDB or Mongoose were the issue - however - my problem was that connect-mongodb-session had been updated and starting slinging the same error. The solution, in this case, was to simply rollback the version of connect-mongodb-session from version 2.3.0 to 2.2.0.

mongoose.connect(process.env.CONNECTION_MONGO, {
useNewUrlParser: true,
useUnifiedTopology: true,
useCreateIndex: true,
useFindAndModify: false,
})
.then(() => {
console.log("Database connected...");
})
.catch((err) => console.error(err));
发布评论

评论列表(0)

  1. 暂无评论