意思是:得到错误400(错误请求)的POST [英] MEAN : Getting Error 400 (Bad Request) on POST

查看:238
本文介绍了意思是:得到错误400(错误请求)的POST的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是相当新的MEAN堆栈,并按照本教程后:的 HTTP://www.bradon$c$c.com/tutorials/mean-stack-tutorial-part-4-angularjs/ ,我努力使自己的Web应用程序发送和接收发票。我有一个本地主机:3000 /发票页面设置这是目前空([])。我试图填写表格,并张贴到其本地主机:3000 /发票,这样MongoDB的数据库被填充。我的MongoDB进程持续运行。这是模型概述我需要在记录的所有字段:

I am fairly new to the MEAN Stack, and after following this tutorial : http://www.bradoncode.com/tutorials/mean-stack-tutorial-part-4-angularjs/, I was trying to make my own web application to send and receive invoices. I have a localhost:3000/invoices page set up which is currently empty ([ ]). I am trying to fill out a form and post to it localhost:3000/invoices so that the MongoDB database is populated. My MongoDB process is constantly running. This is the Model outlining all the fields I require in a record :

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

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

This is my invoice client side controller :

'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一个简单的形式:

And this is a simple form in AngularJS on the front-end :

<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>

当我开始并提交表单,我得到以下几点:

When I proceed and submit the form I get the following :

在这里输入的形象描述

金额为必填字段为明显通过模型,我总是一定填写该值。我已经输入的数字,总是小数,却又是说,现场需要,并显示错误的请求。此外,每当我开始故宫,我的应用程序加载时没有任何错误。我试图看几个YouTube上的教程,但没有明确其原因不工作。斯威夫特帮助将不胜AP preciated:)

Amount is a required field as evident through the model, and I am always certain to fill in that value. I have typed numbers and decimals always, yet is says that field is required and displays Bad Request. Also whenever I do npm start, my application loads up without any errors. I have attempted watching several youtube tutorials, but none clarify the reason for this not working. Swift Help would be greatly appreciated :)

编辑:当我从模型取出所需,交易经历,但没有输入到表单中的值的记录,只是发票日期是

EDIT : When I remove the required from the Model, the transaction goes through, but none of the values inputted into the form are recorded, only the invoice date is.

推荐答案

您得到一个日期,因为那里是一个缺省设置。定义 $ scope.invoice = {} 在你的控制器,这样势必视图(2路)。然后,更改从$ scope.invoice全部取。例如:

You get a date because there is a default set for that. Define $scope.invoice = {} in your controller so that it is bound to your view (2-way). Then, change all fetches from $scope.invoice. For example:

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

这篇关于意思是:得到错误400(错误请求)的POST的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆