0
我是Node.js的新手,試圖找出一些問題。 我在Node.js服務器上爲在MongoDB上寫入用戶信息的Android應用程序定義了註冊函數。未處理的承諾拒絕(拒絕ID:1) - Node.js
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 =用戶;
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 !' });
}
});
})};
...
它編譯沒有任何錯誤。 當我嘗試通過郵差送一個JSON這樣的:
{
"userName" : "username",
"email" : "[email protected]",
"password" : "blabla1"
}
它報告
UnhandledPromiseRejectionWarning:未處理的承諾拒絕(拒絕ID:1)目標對象]
我已經讀過大量的帖子,但我無法擺脫這一點。 有什麼幫助嗎? 謝謝。
份額在您使用註冊函數的代碼。 –
@ mehta-rohan我只需添加路線 – JackLametta
@ mehta-rohan,並從郵遞員(POST - http:// localhost:3000 /註冊) – JackLametta