【问题标题】:(node:71307) [DEP0079] DeprecationWarning(节点:71307)[DEP0079] 弃用警告
【发布时间】:2018-12-28 09:13:00
【问题描述】:

尝试更新 MongoDB 文档获取弃用警告为

(node:71307) [DEP0079] DeprecationWarning: 自定义检查功能 不推荐使用通过 .inspect() 处理对象

节点版本v10.5.0, 数据库版本 v3.6.5, 猫鼬版mongoose@4.1.12

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

【问题讨论】:

  • 请提及您的节点和猫鼬版本。
  • 猫鼬版?
  • 猫鼬版 mongoose@4.1.12
  • 这种减弱现在会出现在猫鼬中。他们会做这方面的工作。现在它可以安全地使用它。没什么好担心的。

标签: javascript node.js mongodb mongoose mongoose-schema


【解决方案1】:

你不必担心这个错误,这是猫鼬警告。实际上 Mongoose 使用 inspect() 来调试输出。他们可能会在节点 12.x 之前更新它。目前使用它是安全的。

不用担心。

检查此信息。 https://nodejs.org/api/deprecations.html#deprecations_dep0079_custom_inspection_function_on_objects_via_inspect

DEP0079:通过 .inspect()# 对对象进行自定义检查功能# 类型: 运行时

在对象上使用名为 inspect 的属性来指定自定义 util.inspect() 的检查功能已弃用。采用 util.inspect.custom 代替。为了向后兼容 Node.js 在 6.4.0 版本之前,可以同时指定两者。

如果您想了解更多详细信息,请参阅此。这正在进行中。警告将出现在节点 10

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

【讨论】:

  • 我的猫鼬版本是 5.5.3 仍然出现错误。即使添加了这一行
【解决方案2】:

为了不收到弃用消息,您可以根据此Github mongoose issue 升级到 mongoose 版本 5.2.10 或更高版本,并在代码中的适当位置设置以下内容:

mongoose.set('useCreateIndex', true)

【讨论】:

  • 我的猫鼬版本是 5.5.3 仍然出现错误。即使添加了这一行
【解决方案3】:

升级到5.2.10并设置

  mongoose.set('useCreateIndex', true);

【讨论】:

    【解决方案4】:

    另一种设置方法是...

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

    更多信息可以在这里找到:https://github.com/Automattic/mongoose/issues/6890

    【讨论】:

      【解决方案5】:

      要解决这个问题,你必须使用useNewUrlParser 和useCreateIndex。请参阅下面的代码。

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

      或者

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

      【讨论】:

        【解决方案6】:

        您应该将选项useCreateIndex 添加到您的连接方法中

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

        【讨论】:

          【解决方案7】:

          完全随机,但至少不是模仿答案:当我不小心使用 Model.find() 而不是 Model.findOne() 时,我收到了这个弃用警告(和意外行为)

          所以我的错误代码看起来像这样

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

          ...对于一个普通的对象/数组,这会因TypeError: Cannot read property 'confirmToken' of undefined 而失败,但对于一个 mongo 文档数组,显然它会执行此检查操作,现在会给出此弃用警告。

          【讨论】:

            【解决方案8】:

            我想在此线程中添加它可能还与其他依赖项有关。

            例如,我为 NodeJS、MongoDB 或 Mongoose 更新或设置的内容都不是问题 - 然而 - 我的问题是 connect-mongodb-session 已更新并开始出现相同的错误。在这种情况下,解决方案是简单地将connect-mongodb-session 的版本从版本2.3.0 回滚到2.2.0

            【讨论】:

              【解决方案9】:
              mongoose.connect(process.env.CONNECTION_MONGO, {
              useNewUrlParser: true,
              useUnifiedTopology: true,
              useCreateIndex: true,
              useFindAndModify: false,
              })
              .then(() => {
              console.log("Database connected...");
              })
              .catch((err) => console.error(err));
              

              【讨论】:

              • 虽然此代码可能会为问题提供解决方案,但最好添加有关其工作原理/方式的上下文。这可以帮助未来的用户学习并最终将这些知识应用到他们自己的代码中。在解释代码时,您也可能会得到用户的积极反馈/赞成。
              猜你喜欢
              • 2016-11-03
              • 2020-03-08
              • 2021-09-27
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2013-09-11
              相关资源
              最近更新 更多