2017-02-22 146 views
0

我越來越deprecatingWarning即使我有:DeprecatingWarning:與貓鼬

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

現在,當我與郵差測試這個我的控制檯給我這個錯誤:

DeprecationWarning: Mongoose: mpromise (mongoose's default promise library) is deprecated, plug in your own promise library instead: http://mongoosejs.com/docs/promises.html 

,正因爲如此,它無法正常工作。 有很多人有這種錯誤,但添加Promise爲他們解決了這個問題。我不明白爲什麼它現在爲我工作。

回答

0

你必須通過承諾庫mongoose.Promise everywere你要求貓鼬,例如。在app.js,在shcema /模型文件,在測試文件

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

除了使用原生global.Promise我recoomended使用藍鳥承諾庫,它的速度更快:https://softwareengineering.stackexchange.com/questions/278778/why-are-native-es6-promises-slower-and-more-memory-intensive-than-bluebird

+0

謝謝,我不知道我必須把它放在任何地方。 – user3450754

0

我發現:

"Yes, it appears that setting the mongoose. Promise in the schema files where mongoose.model is exported eliminates the warning in mongoose versions 4.8 and later."

我想你應該再次檢查你的版本。

+0

感謝@chridam! – NguyenTungs