【问题标题】:MEAN : Getting Error 400 (Bad Request) on POST意思:在 POST 上收到错误 400(错误请求)
【发布时间】:2015-10-20 22:38:49
【问题描述】:

我是 MEAN Stack 的新手,在学习了本教程后:http://www.bradoncode.com/tutorials/mean-stack-tutorial-part-4-angularjs/,我试图制作自己的 Web 应用程序来发送和接收发票。我设置了一个 localhost:3000/invoices 页面,该页面当前为空 ([ ])。我正在尝试填写表格并将其发布到 localhost:3000/invoices 以便填充 MongoDB 数据库。我的 MongoDB 进程一直在运行。这是一个模型,概述了我在记录中需要的所有字段:

'use strict';

/**
 * Module dependencies.
 */
var mongoose = require('mongoose'),
    Schema = mongoose.Schema,
    validation = require('./validation.server.model');

/**
 * Invoices Schema
 */
var InvoiceSchema = new Schema({
    invoice_date: {
        type: Date,
        default: Date.now
    },
    from_entity: {
        type: String,
        default: '',
        trim: true,
        required: 'legal entity cannot be blank'
    },
    from_service: {
        type: String,
        default: '',
        trim: true,     
        required: 'service line cannot be blank'
    },

    country: {
        type: String,
        default: '',
        trim: true,
        required: 'country cannot be blank'
    },

    to_entity: {
        type: String,
        default: '',
        trim: true,
        required: 'legal entity cannot be blank'
    },

    to_service: {
        type: String,
        default: '',
        trim: true,     
        required: 'service line cannot be blank'

    },

    partner: {
        type: String,
        default: '',
        trim: true,     
        required: 'partner cannot be blank'
    },

    invoice_number: {
        type: String,
        default: '',
        trim: true,     
        unique: true,
        required: 'invoice number cannot be blank'
    },

    currency: {
        type: String,
        default: '',
        trim: true,
        required: 'currency cannot be blank'
    },


    amount: {
        type: Number,
        default: '',
        trim: true,
        required: 'amount cannot be blank'

    }

});

mongoose.model('Invoice', InvoiceSchema);

这是我的发票客户端控制器:

'use strict';

// Invoices controller
angular.module('invoices').controller('InvoicesController', ['$scope', '$stateParams', '$location', 'Authentication', 'Invoices', '$filter',
    function($scope, $stateParams, $location, Authentication, Invoices, $filter) {
        $scope.authentication = Authentication;
        $scope.currentPage = 1;
        $scope.pageSize = 10;
        $scope.offset = 0;

        // Page changed handler
        $scope.pageChanged = function() {
            $scope.offset = ($scope.currentPage - 1) * $scope.pageSize;
        };

        // Create new Invoice
        $scope.create = function() {
            var invoice = new Invoices ({
                    amount: this.amount,
                    invoice_number: this.invoice_number,
                    partner: this.partner,
                    to_service: this.to_service,
                    to_entity: this.to_entity,
                    country: this.country,
                    from_service: this.from_service,
                    from_entity: this.from_entity
            });

            // Redirect after save
            invoice.$save(function(response) {
                $location.path('invoices/' + response._id);

                // Clear form fields
                $scope.name = '';
            }, function(errorResponse) {
                $scope.error = errorResponse.data.message;
            });
        };

        // Remove existing Invoice
        $scope.remove = function(invoice) {
            if (invoice) {
                invoice.$remove();

                for (var i in $scope.invoices) {
                    if ($scope.invoices [i] === invoice) {
                        $scope.invoices.splice(i, 1);
                    }
                }
            } else {
                $scope.invoice.$remove(function() {
                    $location.path('invoices');
                });
            }
        };

        // Update existing Invoice
        $scope.update = function() {
            var invoice = $scope.invoice;

            invoice.$update(function() {
                $location.path('invoices/' + invoice._id);
            }, function(errorResponse) {
                $scope.error = errorResponse.data.message;
            });
        };


        // Find a list of Invoices
        $scope.find = function() {
                $scope.invoices = Invoices.query();
        };

        // Find existing Invoice
        $scope.findOne = function() {
            $scope.invoice = Invoices.get({ 
                invoiceId: $stateParams.invoiceId
            });
        };


        // Search for a Invoice
        $scope.invoiceSearch = function(invoice) {
            $location.path('invoices/' + invoice._id);
        };
    }
]);

这是前端AngularJS中的一个简单表单:

<section data-ng-controller="InvoicesController">
    <div class="page-header">
        <h1>New Invoice</h1>
    </div>
    <div class="col-md-12">
        <form class="form-horizontal" data-ng-submit="create()" novalidate>
            <fieldset>
                <div class="form-group">
                    <label class="control-label" for="invoice_number">Invoice Number</label>
                    <div class="controls">
                        <input type="text" data-ng-model="invoice.invoice_number" id="invoice_number" class="form-control" placeholder="Invoice Number" required>
                    </div>
                </div>

                <div class="form-group">
                    <label class="control-label" for="amount">Amount</label>
                    <div class="controls">
                        <input type="text" data-ng-model="invoice.amount" id="amount" class="form-control" placeholder="Amount" required>
                    </div>
                </div>

                <div class="form-group">
                    <label class="control-label" for="currency">Currency</label>
                    <div class="controls">
                        <input type="text" data-ng-model="invoice.currency" id="currency" class="form-control" placeholder="Currency" required>
                    </div>
                </div>

                <div class="form-group">
                    <label class="control-label" for="partner">Partner</label>
                    <div class="controls">
                        <input type="text" data-ng-model="invoice.partner" id="partner" class="form-control" placeholder="Partner" required>
                    </div>
                </div>

                <div class="form-group">
                    <label class="control-label" for="country">Country</label>
                    <div class="controls">
                        <input type="text" data-ng-model="invoice.country" id="name" class="form-control" placeholder="Country" required>
                    </div>
                </div>

                <div class="form-group">
                    <label class="control-label" for="to_service">Service Line (To:)</label>
                    <div class="controls">
                        <input type="text" data-ng-model="invoice.to_service" id="to_service" class="form-control" placeholder="Service Line (To:)" required>
                    </div>
                </div>

                <div class="form-group">
                    <label class="control-label" for="from_service">Service Line (From:)</label>
                    <div class="controls">
                        <input type="text" data-ng-model="invoice.from_service" id="from_service" class="form-control" placeholder="Service Line (From:)" required>
                    </div>
                </div>
                <div class="form-group">
                    <label class="control-label" for="to_entity">Entity(To:)</label>
                    <div class="controls">
                        <input type="text" data-ng-model="invoice.to_entity" id="to_entity" class="form-control" placeholder=" Entity (To:)" required>
                    </div>
                </div>

                <div class="form-group">
                    <label class="control-label" for="from_entity">Entity(From:)</label>
                    <div class="controls">
                        <input type="text" data-ng-model="invoice.from_entity" id="from_entity" class="form-control" placeholder=" Entity (From:)" required>
                    </div>
                </div>

                <div class="form-group">
                    <input type="submit" class="btn btn-default">
                </div>
                <div data-ng-show="error" class="text-danger">
                    <strong data-ng-bind="error"></strong>
                </div>
            </fieldset>
        </form>
    </div>
</section>

当我继续并提交表单时,我得到以下信息:

从模型中可以看出,金额是必填字段,我总是确定填写该值。我总是输入数字和小数,但据说该字段是必需的并显示错误请求。此外,每当我执行 npm start 时,我的应用程序都会加载而没有任何错误。我曾尝试观看几个 youtube 教程,但没有一个说明这不起作用的原因。 Swift 帮助将不胜感激 :)

编辑:当我从模型中删除所需时,交易通过,但没有记录输入到表单中的值,只有发票日期。

【问题讨论】:

    标签: javascript angularjs mongodb model-view-controller mean-stack


    【解决方案1】:

    您会得到一个日期,因为有一个默认设置。在您的控制器中定义$scope.invoice = {},使其绑定到您的视图(2 向)。然后,更改 $scope.invoice 中的所有提取。例如:

        $scope.invoice = {};
        $scope.create = function() {
            var invoice = new Invoices ({
                    amount: $scope.invoice.amount,
                    invoice_number: $scope.invoice.invoice_number,
                    partner: $scope.invoice.partner,
                    ...
            });
            ...
        };
    

    【讨论】:

    • 我通过删除发票让它工作了。在 "data-ng-model="invoice.currency"" 中。你的方法更好吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-10-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-04
    • 2017-12-24
    • 1970-01-01
    相关资源
    最近更新 更多