MEAN:在POST上获取错误400(错误请求)

沙申克

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

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

在此处输入图片说明

从模型可以看出,金额是必填字段,我始终确定要填写该值。我总是输入数字和小数,但是说该字段是必填字段,并显示Bad Request。同样,每当我执行npm启动时,我的应用程序就会加载而不会出现任何错误。我曾尝试观看一些youtube教程,但都没有弄清这种方法不起作用的原因。迅速的帮助将不胜感激:)

编辑:当我从模型中删除所需的内容时,交易会进行,但是不会记录输入到表单中的任何值,仅记录发票日期。

TJ-

您会得到一个日期,因为有一个默认设置。$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,
                ...
        });
        ...
    };

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

意外错误400:错误请求HttpBuilder POST请求

来自分类Dev

尝试获取POST以返回400错误请求

来自分类Dev

在Android中使用Asmack获取错误的请求400错误

来自分类Dev

错误请求(错误400)在Android中获取静态地图

来自分类Dev

在每个POST上返回400错误请求-fosrestbundle,Symfony3

来自分类Dev

AJAX POST请求中的HTTP错误400

来自分类Dev

AngularJS中的POST 400错误请求

来自分类Dev

httr对API的POST请求返回400错误

来自分类Dev

耗时POST请求始终返回400错误请求

来自分类Dev

(400)错误的请求:获取Salesforce的AccessToken错误:“ invalid_grant”

来自分类Dev

与PayPal连接:获取状态400(错误请求)

来自分类Dev

Django Localhost上的错误请求(400)

来自分类Dev

Python请求POST-400-Python请求的错误请求

来自分类Dev

在Express.js上使用Axios向Spotify API发出POST请求时出现错误400

来自分类Dev

URL中的参数获取400错误的请求错误

来自分类Dev

POST方法是ajax给出400(错误请求)

来自分类Dev

错误请求(错误400)在Android中获取静态地图

来自分类Dev

AJAX POST中的400错误的请求错误

来自分类Dev

WCF Post方法,返回400错误请求

来自分类Dev

HttpURLConnection POST返回400错误请求

来自分类Dev

AJAX错误:400错误的请求

来自分类Dev

带有python 400错误请求的POST请求

来自分类Dev

$http post 方法的 400 错误请求

来自分类Dev

POST 400错误,ajax请求

来自分类Dev

在 Python 3 中使用 Socket 获取 400 错误请求错误

来自分类Dev

烧瓶错误请求错误 400

来自分类Dev

Python POST 请求 400 错误请求

来自分类Dev

错误的请求错误 400

来自分类Dev

C++ Http POST 400 错误请求

Related 相关文章

热门标签

归档