0

我對MEAN堆棧相當陌生,在學習本教程之後:http://www.bradoncode.com/tutorials/mean-stack-tutorial-part-4-angularjs/,我試圖製作自己的Web應用程序來發送和接收發票。我有一個localhost:3000/invoices頁面,它目前是空的([])。我正在嘗試填寫表單併發布到localhost:3000/invoices,以便填充MongoDB數據庫。我的MongoDB進程正在不斷運行。這是模型概述我需要在記錄的所有字段:MEAN:在開機自檢後出現錯誤400(錯誤的請求)

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

當我開始並提交表單我得到如下:

enter image description here

金額是通過模型顯而易見的必填字段,我總是肯定會填寫該值。我總是輸入數字和小數,但是表示該字段是必需的,並顯示錯誤的請求。此外,每當我做npm開始,我的應用程序加載沒有任何錯誤。我曾試圖觀看過幾個YouTube教程,但沒有一個澄清了這個不起作用的原因。 Swift幫助將不勝感激:)

編輯:當我從模型中刪除所需的,交易通過,但沒有輸入到表格中的值都記錄下來,只有發票日期是。

回答

1

你得到一個日期,因爲有一個默認的設置。在您的控制器中定義$scope.invoice = {},以便它綁定到您的視圖(雙向)。然後,從$ 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, 
       ... 
     }); 
     ... 
    }; 
+0

我通過刪除發票得到它的工作。在「data-ng-model =」invoice.currency「」中。你的方法更好嗎? – Shashank

相關問題