在一个post方法角度6 +节点JS中传递多个参数

时间:2018-07-24 16:54:53

标签: node.js angular mongoose

我正在使用Node JS + Mongoose在angular 6中构建一个应用程序。 我想通过一个发布请求将两个参数传递给我的后端。

我的问题是,我可以在一个POST请求中传递它们吗? 谢谢

这些是参数:

   public order:Order  // Json
   public takenSeatsIds: any // Arrey of objects [{"id" : 5},{"id": 6}]

我想将order作为json传递并将其设置在此模块中

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

    var showHallsSchema = new mongoose.Schema({

        clientName: {
            type:String,
            required: true
        },

        clientLastName: {
            type:String,
            required: true
        },


        clientEmail: {
            type:String,
            required: true
        },


        slectedSeats: {
            type:Array ,
            required: true
        },

        showId: [
            {type: Schema.Types.ObjectId,
             ref: "Shows"    
            }]


    })

module.exports = mongoose.model('Orders', showHallsSchema)

和我想作为对象传递的takenSeatsIds并设置为该模块其中一个字段内的值

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


var showSchema = new mongoose.Schema({

     showsHall: [{
         type: Schema.Types.ObjectId,
         ref: "ShowHall"    
        }],

     movie: [{
         type: Schema.Types.ObjectId,
         ref:"Movies" 
        }],

    showDate: String,

    showStartTime: String,

    showEndTime: String,

    movieName: String,

        takenSeats: {
        type:Array ,
        required: true
    },



})

var Shows = module.exports = mongoose.model('Shows', showSchema);

这是我的有角度的邮政服务(目前我仅在我的POST请求中传递order

import { Injectable } from "../../../node_modules/@angular/core";
import { HttpClient, HttpParams } from '@angular/common/http';
import { Order } from "../models/order-model";


@Injectable()

export class OrderManagerService {
    showApiUrl = "http://localhost:3000/orders";

   public seatsdetails:any
   public takenSeatsIds: any 
   public order:Order
   public showInfo: any
   constructor(private http: HttpClient){}


 // Set data
   setOrderedSeatInfo(orderDetails: Object){
       this.seatsdetails = orderDetails             
   }

   setOrderedSeatIdInfo(takenSeatsDetails: Object){
       this.takenSeatsIds = takenSeatsDetails
   }

   setShowInfo(showInfo: Object){
       this.showInfo = showInfo
       console.log("service" + showInfo)
   }

// Get data

   getSelectedSeats(){
    return this.seatsdetails
   }

   getShowInfo(){
       return this.showInfo
   }

   sendDataToServer(userName: string, userLastName:string, userEmail: string ){
        this.order = new Order (userName,userLastName,userEmail,this.showInfo._id,this.seatsdetails )
        console.log("console" + this.order)
        var data = JSON.stringify(this.order);
        return this.http.post(this.showApiUrl, this.order)       
   }


}

我的路线

router.post('/orders', function(req,res,next){
    Order.create(req.body, function(err, createdOrder){
        if (err) return next(err);
        console.log(createdOrder)
        res.json(createdOrder)
    })
})

1 个答案:

答案 0 :(得分:0)

this.http.post(this.showApiUrl, {order: this.order, takenSeatsIds:this.seatsdetails})