angular2 http.post请求请求正文为空

时间:2018-08-22 11:08:33

标签: angular mongodb http post

我的angular 2应用程序出现问题,我试图将json oberts发送到我的mongofb数据库,当我单击该帖子时,它将_id发送到我的mongodb,但是我的req.body为空

import { Injectable } from '@angular/core';
import { HttpModule ,Http , Headers  } from '@angular/http';
import 'rxjs/add/operator/map';
 
 
 

@Injectable()
export class BookService {

    constructor(private _http:Http){

    }

    getTodos(){
        return this._http.get('/api/v1/book')
            .map(res => res.json());
    }

    saveBooking(booking){
        var headers = new Headers();
        headers.append('Content-Type', 'application/json');

                 
      
        return this._http.post('/api/v1/book' , { b: 'texts.value',
      isCompleted: 'b'}, {headers: headers})
           .map(res => res.json());
            
    }
    
}

这是我的router.js

var express = require('express');
var router = express.Router();

var mongojs = require('mongojs');
var db = mongojs('mongodb://lee:Zzbawsoldd1@ds163630.mlab.com:63630/bookings_drbookings',['bookings']);

router.get('/book', function(req,res,next){

    db.bookings.find(function(err, bookings){
        if(err){
            res.send(err);
        }else{
            res.json(bookings);
        }
        
    });
});

// Get Single Todo
router.get('/book/:id', function(req, res, next){
    db.bookings.findOne({
        _id: mongojs.ObjectId(req.params.id)
    }, function(err, todo){
        if(err){
           res.send(err); 
        } else {
           res.json(todo);
        }
    });
});




router.post('/book', function(req, res, next){
    /*if(!todo.text || !(todo.isCompleted + '')){
        res.status(400);
        res.json({
            "error": "Invalid Datazz"
        });
    } else {*/
        db.bookings.save(req.body, function(err, result){
            if(err){
                res.send(err); 
            } else {
                res.json(result);
            }
        });
    
});



router.put('/book/:id', function(req, res, next){
    var bookinfo = req.body;
    var updObj = {};
    
    if(bookinfo.isCompleted){
       updObj.isCompleted = bookinfo.isCompleted;
    }
    
    if(bookinfo.text){
        updObj.text = bookinfo.text;
    }
    
    if(!updObj){
        res.status(400);
        res.json({
            "error": "Invalid Data"
        });
    } else {
        db.bookings.update({
            _id: mongojs.ObjectId(req.params.id)
        },updObj, {}, function(err, result){
            if(err){
                res.send(err); 
            } else {
                res.json(result);
            }
        });
    }
});

router.delete('/book/:id', function(req, res, next){
    db.bookings.remove({
        _id: mongojs.ObjectId(req.params.id)
    },'', function(err, result){
        if(err){
            res.send(err); 
        } else {
            res.json(result);
        }
    });
});
module.exports = router;

我对服务器的请求正在运行,但是它没有将json对象从我的服务发送到router.js

2 个答案:

答案 0 :(得分:0)

尝试下面的代码捕捉:

  saveBooking(booking){
    var headers = new Headers();
    headers.append('Content-Type', 'application/json');
    let data:any={};
    data.b='texts.value';
    data.isCompleted='b';
    return this.http.post('/api/v1/book' , data, {headers: headers}) .map(res =>  res.json());
}

答案 1 :(得分:0)

谢谢您的帮助,但我找到了解决方案

在我写的app.set而不是快速服务器代码中的app.use的server.js文件中存在错误