【发布时间】:2015-08-09 06:17:35
【问题描述】:
我想在 $resource 方法中将一个数组传递给 $save,然后在我的服务器控制器中接收它以修改数据库中的数据。
我可以在 GET 中传递一个变量,它工作得很好! 但是我想传递的数组似乎不起作用!
我的控制台没有收到任何错误。
我正在使用 MEAN.js 并且 POST 和 GET 请求似乎都可以正常工作(绿色 200 )但是当我调用 req.body.variable1 来访问我通过 $save 方法传递的变量时,它不起作用。
我可以在同一页面上同时使用 POST 和 GET 方法吗?
我将不胜感激任何帮助或提示,这将导致正确的答案。 谢谢!
资源工厂
angular.module('alls').factory('Alls', ['$resource', function($resource) {
return $resource('alls/:issuename', {issuename: '@issuename'},
{
get:{ method: 'GET', isArray:true }
});
}]);
客户端控制器
angular.module('alls').controller('AllsController', ['$scope', '$stateParams', '$location', 'Authentication', 'Alls',
function($scope, $stateParams, $location, Authentication, Alls ) {
$scope.search = function(){
Alls.get({issuename: $scope.issueinput},function(response){
$scope.alls = response;
});
var myArray = new Alls ({
variable1: 'Blue',
variable2: 'Red'
});
myArray.$save();
};
} ]);
服务器路由
module.exports = function(app) {
var alls = require('../../app/controllers/alls.server.controller');
app.route('/alls')
.get(alls.list)
.post(alls.list);
app.route('/alls/:issuename')
.get(alls.list)
.post(alls.list); app.param('issuename', alls.searchfun); };
服务器控制器
exports.searchfun = function(req, res, next, id) {
req.alpha = id;
next();
};
exports.list = function(req, res) {
var colorVar = req.body.variable1;
var searchParam = req.alpha;
mongoose.model(searchParam).find({ color: colorVar }).sort('-created').exec(function(err, alls) {
res.jsonp(alls);
}); };
【问题讨论】:
标签: javascript angularjs node.js express mean-stack