Node.js 不能';t在平均堆栈中使用http post方法获取我正在发布的对象
我的代码是(server.js): Mongoose模式(我认为它没有任何错误)(customer.js): 我的控制器部分:Node.js 不能';t在平均堆栈中使用http post方法获取我正在发布的对象,node.js,express,post,mongoose,mean-stack,Node.js,Express,Post,Mongoose,Mean Stack,我的代码是(server.js): Mongoose模式(我认为它没有任何错误)(customer.js): 我的控制器部分: var myApp = angular.module('myApp', []); myApp.controller('AppCtrl', ['$scope', '$http', function($scope, $http) { console.log("Hello World from controller"); var refresh = function
var myApp = angular.module('myApp', []);
myApp.controller('AppCtrl', ['$scope', '$http', function($scope, $http) {
console.log("Hello World from controller");
var refresh = function() {
$http.get('/customers').success(function(response) {
console.log("I got the data I requested");
//console.log(response);
$scope.customers = response;
$scope.customer = "";
});
};
refresh();
$scope.addCustomer = function() {
console.log($scope.customer);
$http.post('/customers', $scope.customer).success(function(response) {
console.log(response);
refresh();
});
};
疑问:在app.post()中,显示的输出如下所示:(post被打印,req.body被打印,但是当I console.log(自定义)时,只有对象id被打印,没有其他内容,就好像req.body没有保存在自定义对象中一样)
我找到了错误所在,不用担心,所有代码都很好。将此添加到app.use(bodyParser.json())行上方:
app.use(bodyParser.urlencoded({
extended: true
});
在我添加了这一点之后,它起了作用,干杯。当您使用res.json(自定义)而不是res.send(自定义)时会发生什么,我们可能会得到更多详细信息。同上。没有别的。唯一的问题是,在运行custom.save之后,我只得到{{uu v:0,{u id:58bb591a7cdbe0b53400001}作为输出。req.body部分未保存在自定义对象中。看起来似乎其他项确实不存在,控制台日志类似于req.body.email+“”+req.body.name,而不是您的“已创建”消息,让我们看看它是否显示“未定义”是正确的。在我的mongoDB中,也只显示字段_id和_v,没有任何req.body部分被保存到其中。
var myApp = angular.module('myApp', []);
myApp.controller('AppCtrl', ['$scope', '$http', function($scope, $http) {
console.log("Hello World from controller");
var refresh = function() {
$http.get('/customers').success(function(response) {
console.log("I got the data I requested");
//console.log(response);
$scope.customers = response;
$scope.customer = "";
});
};
refresh();
$scope.addCustomer = function() {
console.log($scope.customer);
$http.post('/customers', $scope.customer).success(function(response) {
console.log(response);
refresh();
});
};
POST:
{ name: 'ds',
mobile: 'adas',
phone: 'dasd',
address: 'asd',
dob: 'asdas',
email: 'das' }
{ __v: 0, _id: 58bb591a7cdbe0b534000001 }
created
app.use(bodyParser.urlencoded({
extended: true
});