【问题标题】:couldn't get the object which I am posting using http post method in MEAN Stack无法获取我在 MEAN Stack 中使用 http post 方法发布的对象
【发布时间】:2017-07-24 23:55:44
【问题描述】:

我的代码是(server.js):

var express = require('express');
var bodyParser = require('body-parser');
var customerr = require('./customer')
var mongoose = require('mongoose');
mongoose.connect('mongodb://127.0.0.1:27017/customers');

app = express();
app.use(express.static(__dirname + '/public'));
app.use(bodyParser.json());

app.post('/customers', function (req,res){
  console.log("POST: ");
  console.log(req.body);
  var custom = new customerr({
    name : req.body.name,
    mobile : req.body.mobile,
    phone : req.body.phone,
    address :req.body.address,
    dob : req.body.dob,
    email : req.body.email});

    custom.save(function (err) {
    if (!err) {
      console.log(custom);                     // doubt in this line
      console.log("created");
    } else {
      return console.log(err);
    }
  });
  return res.send(custom);
});

Mongoose 架构(我认为这没有任何错误。)(customer.js):

var mongoose = require('mongoose');
var Schema = mongoose.Schema;  

var customerSchema = new Schema({  
    Name: { type: String }, 
    Mobile:  { type: String },  
    Number: { type: String },
    Address: { type: String },
    DOB: { type: String },
    Email:{ type: String }
});
var customerr = mongoose.model('customerr', customerSchema);  
module.exports =customerr;

我的控制器部分:

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 被打印,但是当我 console.log(custom) 只打印对象 ID 时,就好像 req .body 没有保存在自定义对象中)

POST: 
{ name: 'ds',
  mobile: 'adas',
  phone: 'dasd',
  address: 'asd',
  dob: 'asdas',
  email: 'das' }
{ __v: 0, _id: 58bb591a7cdbe0b534000001 }
created

【问题讨论】:

  • 当您执行 res.json(custom) 而不是 res.send(custom) 时会发生什么,我们可能会获得更多详细信息。
  • 同上。没有别的。
  • 唯一的问题是,运行 custom.save 后,我只得到 { __v: 0, _id: 58bb591a7cdbe0b534000001 } 作为输出。 req.body 部分未保存在自定义对象中。
  • 看起来好像其他项目不存在,控制台记录类似 req.body.email + ' ' + req.body.name 而不是你的“创建”消息,让我们看看是否它说'未定义'
  • 是的,你说得对。在我的 mongoDB 中,也只有字段 _id 和 _v 显示,没有 req.body 部分被保存到其中。

标签: node.js express post mongoose mean-stack


【解决方案1】:

我找到了错误所在,不用担心您的所有代码都没有问题。在 app.use(bodyParser.json()) 行上方添加:

app.use(bodyParser.urlencoded({
    extended: true
});

在我添加之后它起作用了,干杯。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-10-28
    • 1970-01-01
    • 1970-01-01
    • 2020-05-07
    • 2019-02-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多