我在Node.js中有这个端点:
usersRoute.get('/get', function(req, res) {
//If no date was passed in - just use todays date
var date = req.query.date || dateFormat(new Date(), 'yyyy-mm-dd'),
search = req.query.search;
users.getAllUsers(date, search)
.then(function(results) {
res.json(results);
}, function(err) {
res.status(500).json({
success: false,
message: 'Server error.',
data: []
});
});
});
出于测试目的,我错误地命名了我的SQL表,因此它将转到function(err){}
我的服务中的功能如下所示
function getUsers(date, search) {
return $http.get('/api/users/get', {
params: {
date: UtilsService.formatDate(date),
search: search
}
})
.then(getData)
.catch(handleErr);
function getData(response) {
console.log(response);
return response.data;
}
function handleErr(err) {
console.log(err);
LoggerService.error('Could not retrieve users.', err ,'Ooops');
return $q.reject(err);
}
}
console.log
和then
中的catch
都返回undefined
。
如果有人能向我解释为什么会这样,我会很感激。
修改澄清;以下是更多后端代码:
Users.js
var sql = require('seriate');
var when = require('when');
var getAllUsers = function(date, search) {
return sql.execute({
query: sql.fromFile('../sql/users/getAllUsers.sql'),
params: {
date: {
type: sql.DATE,
val: date
},
search: {
type: sql.VARCHAR,
val: search
}
}
});
}
module.exports = {
getAllUsers: getAllUsers
};
UsersApi.js
var express = require('express');
var dateFormat = require('dateformat');
var users = require('../models/users');
var validation = require('../middleware/validation');
var usersRoute = express.Router();
//Checks for a valid token
validation.isLoggedIn(usersRoute);
usersRoute.get('/get', function(req, res) {
//If no date was passed in - just use todays date
var date = req.query.date || dateFormat(new Date(), 'yyyy-mm-dd'),
search = req.query.search;
users.getAllUsers(date, search)
.then(function(results) {
res.json(results);
}, function(err) {
res.status(500).json({
success: false,
message: 'Server error.',
data: []
});
});
});
module.exports = usersRoute;
在我的服务中,访问了then
部分以及catch
,并且两个对象(response
和err
)都是undefined
答案 0 :(得分:1)
您可以分享全套服务吗?我根据你提供的示例创建了一个调用我在OpenShift上托管的nodejs服务的plunker with your code我创建了一个模拟你的函数的方法(错误):
self.routes['/get'] = function(req, res) {
res.status(500).json({
success: false,
message: 'Server error.',
data: []
});
};
关于plunker的应用程序是:
var app = angular.module('plunker', []);
app.service('UserService', function($http, $q) {
this.getUsers = function(date, search) {
return $http.get('https://nodejs-starters.rhcloud.com/get', {
params: {
date: date,
search: search
}
})
.then(getData)
.catch(handleErr);
}
function getData(response) {
console.log('status: ' + reponse.status + ' - ' + response.message);
return response.data;
}
function handleErr(err) {
console.log(JSON.stringify(err));
console.log('status: ' + err.status + ' Err= ' + err.data.message);
console.log('Could not retrieve users.', err.data.message, 'Ooops');
return $q.reject(err);
}
});
app.controller('MainCtrl', function($scope, $http, $q, UserService) {
var vm = $scope;
vm.name = 'World';
UserService.getUsers(new Date(), '*');
});
并且,控制台显示以下结果:
app.js:21 {"data":{"success":false,"message":"Server error.","data":[]},"status":500,"config":{"method":"GET","transformRequest":[null],"transformResponse":[null],"params":{"date":"2016-06-02T18:33:50.633Z","search":"*"},"url":"https://nodejs-starters.rhcloud.com/get","headers":{"Accept":"application/json, text/plain, */*"}},"statusText":"Internal Server Error"}
app.js:22 status: 500 Err= Server error.
app.js:23 Could not retrieve users. Server error. Ooops
因此调用handleErr并定义err。 'err.data.success'为false,'err.data.message'为'服务器错误'。正如你在nodejs上写的那样。
在plunker示例中,您可以将您的网址https://nodejs-starters.rhcloud.com/get替换为您的网址,并查看您是否获得相同的结果或不同的结果?