gpt4 book ai didi

javascript - 使用 Angularjs Expressjs Node 和 MEAN.js 通过 POST 方法将数组传递给 $resource

转载 作者:行者123 更新时间:2023-11-29 21:47:50 26 4
gpt4 key购买 nike

我想在 $resource 方法中将一个数组传递给 $save,然后在我的服务器 Controller 中接收它以修改数据库中的数据。

我可以在 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 }

});
}]);

客户端 Controller

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

服务器 Controller

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

最佳答案

看起来你的 $resource url 和你的 $route 不匹配。根据您的文件夹结构,您需要像这样向您的 $resource url 添加一个斜杠...

return $resource('/alls/:issuename', {issuename: '@issuename'},
{

get:{ method: 'GET', isArray:true }

});
}]);

...或(更有可能)修复您的路线。请参阅此答案以供引用... AngularJS and nodeJs Express: routes and refresh

关于javascript - 使用 Angularjs Expressjs Node 和 MEAN.js 通过 POST 方法将数组传递给 $resource,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30477774/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com