弃用警告:用猫鼬

时间:2017-02-22 08:07:16

标签: node.js mongodb express mongoose

我正在弃用警告,即使我有:

mongoose.Promise = global.Promise;

var express = require('express');
var responseTime = require('response-time');
var mongoose = require('mongoose');
var User = require('../models/user');
var Task = require('../models/task');
var Rating = require('../models/rating');
var Skill = require('../models/skill');
var Match = require('../models/match');
var UserSkill = require('../models/user_skill');
var app = express();
var bodyParser = require('body-parser');
var PORT_NUMBER = 3000;

// response-time middleware (adds X-Response-Header to responses)
app.use(responseTime());
//
mongoose.Promise = global.Promise;
mongoose.connect('mongodb://useyourown');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
  extended: true
}));

app.post('/create', function(req,res){
    let newTask = new Task();

    newTask.name = req.body.name;
    newTask.experience = req.body.experience;
    newTask.user_id = req.body.user_id;
    newTask.rating = req.body.rating;

    newTask.save(function(err, task){
        if(err){
            return err;
        }
        else{
            console.log("Task saved: " + task.name);
        }
        res.redirect('hearbeat');
    });
});

现在,当我使用Postman测试时,我的控制台正在给我这个错误:

DeprecationWarning: Mongoose: mpromise (mongoose's default promise library) is deprecated, plug in your own promise library instead: http://mongoosejs.com/docs/promises.html
因此,它无法正常工作。 有很多人都有这种错误,但是添加Promise为他们修复了它。我不明白为什么它现在适合我。

2 个答案:

答案 0 :(得分:0)

我找到了:

  

"是的,似乎设置了猫鼬。架构中的承诺   导出mongoose.model的文件消除了警告   mongoose版本4.8及更高版本。"

我认为您应该再次查看您的版本。

答案 1 :(得分:0)

你必须将promise库传递给mongoose.Promise每个你需要猫鼬的地方,例如。在app.js中,在shcema / model文件中,在测试文件中

var mongoose = require('mongoose');
mongoose.Promise = global.Promise;

相反使用原生global.Promise我重新使用了Bluebird promise库,速度更快:https://softwareengineering.stackexchange.com/questions/278778/why-are-native-es6-promises-slower-and-more-memory-intensive-than-bluebird