未处理的承诺拒绝(拒绝ID:1) - Node.js

时间:2017-08-10 11:57:30

标签: node.js controller promise postman

我是Node.js的新手,并试图找出一些问题。 我在Node.js服务器上为在MongoDB上写入用户信息的Android应用程序定义了一个注册功能。

server.js

var express = require('express'),
  app = express(),
  port = process.env.PORT || 3000,
  mongoose = require('mongoose'),
  Task = require('./api/models/serverModel'),
  bodyParser = require('body-parser');

var url = "mongodb://localhost:27017/ProjectDB";
mongoose.Promise = global.Promise;
mongoose.connect(url); 

app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());

var routes = require('./api/routes/serverRoute');
routes(app);

app.listen(port);
console.log('Project RESTful API server started on: ' + port);

serverModel

var UserSchema = new mongoose.Schema({
    userName: {type: String, unique: true, required: true},
    email: {type: String, unique: true, required: true},
    password: {type: String, required: true},
    createdAt: {type: String, required: true}
});
var User = mongoose.model('User', UserSchema);

module.exports = User;

serverRoute

module.exports = function(app) {
  var Project = require('../controllers/serverController');

  // Project Routes
    app.route('/signup')
        .post(Project.signup);
};

serverController

...
mongoose.Promise = global.Promise;
exports.signup = function(req, res)  {

return new Promise( function(resolve,reject) {

    var user = new User({
       userName: req.body.name,
       password: req.body.pass,
       mail: req.body.email,
       createdAt : new Date()
    });

user.save()
    .then(function() { resolve({ status: 201, message: 'User Registered Successfully !' })})
    .catch(function(err) {
        if (err.code == 11000) {
            reject({ status: 409, message: 'User Already Registered !' });
        } else {
            reject({ status: 500, message: 'Internal Server Error !' });
        }
    });

})};

...

它编译没有任何错误。 当我尝试通过Postman发送JSON时:

{
  "userName" : "username",
  "email" : "user.name@gmail.com",
  "password" : "blabla1"
}

报告

  

UnhandledPromiseRejectionWarning:未处理的承诺拒绝(拒绝ID:1)[object Object]

我已经阅读了很多帖子,但我无法摆脱这种情况。 有帮助吗? 感谢。

1 个答案:

答案 0 :(得分:0)

试试这个并恢复。

<强> server.js

var express = require('express'),
app = express(),
port = process.env.PORT || 3000,
mongoose = require('mongoose'),
Task = require('./api/models/serverModel'),
bodyParser = require('body-parser');

var url = "mongodb://localhost:27017/ProjectDB";
mongoose.Promise = global.Promise;
mongoose.connect(url); 

app.use(bodyParser.urlencoded({ extended: true })); 
app.use(bodyParser.json());

var routes = require('./api/routes/serverRoute');
//change 
//routes(app);
app.use('/',routes);

app.listen(port);
console.log('Project RESTful API server started on: ' + port);

<强> serverRoute

var express = require('express');
var router = express.Router();

// GET home page. 
router.post('/signup', function(req, res, next) {
    var Project = require('../controllers/serverController');
    Project.signup().then(function(result){
    res.send(result);
  }).catch(function(err){
      res.send(err);
  });
});

module.exports = router;