2017-04-13 69 views
0

空{}我試圖從axios傳遞兩個id(book_id,user_id)到node.js. 當我console.log(req.body)它給我空對象。react axios調用node.js給req.body

來自axios的console.log(「bookid」,book_id,「user」,user_id)。 它返回:

enter image description here

當我CONSOLE.LOG

app.delete('/mypage', function(req, res, next) { 
    console.log("req.body", req.body) 
    db.delete_mylist([req.body.book_id], function(err, individual) { 


     if(err) res.status(500).send(err); 
     else { 
      db.get_mypage_book(function(err, all) { 
       if(err) res.status(500).send(err); 
       else res.send(all); 
      }); 
     } 
    }); 
}); 

它給了我這個

enter image description here

不知道我在做什麼錯在這裏。它在app.post上工作,但它似乎並沒有給我任何關於app.delete的東西。

action.js

import axios from 'axios'; 

export const BOOK_SELECTED = 'BOOK_SELECTED'; 
export const GET_BOOKS = 'GET_BOOKS'; 
export const SIGN_UP_USER = 'SIGN_UP_USER'; 
export const LOGIN_USER = 'LOGIN_USER'; 
export const ADD_MYPAGE = 'ADD_MYPAGE'; 
export const GET_MYPAGE = 'GET_MYPAGE'; 
export const DELETE_BOOK = 'DELETE_BOOK'; 
export const LOGOUT_USER = 'LOGOUT_USER'; 


const ROOT_URL = 'http://localhost:3000/'; 

export function deleteBook(book_id, user_id) { 
    console.log("bookid", book_id, "user", user_id); 
    const request = axios.delete(`${ROOT_URL}mypage`, book_id, user_id); 
    return { 
     type: DELETE_BOOK, 
     payload: request 
    } 
} 

Node.js的

const express = require('express'); 
const bodyParser = require('body-parser'); 
const session = require('express-session'); 
const cors = require('cors'); 
const massive = require('massive'); 
const config = require('./config'); 
const app = module.exports = express(); 

const mass = massive.connectSync({connectionString: config.connectionString}); 

app.use(bodyParser.json()); 
app.use(cors()); 

//db settings 
app.set('db', mass); 
let db = app.get('db'); 

//The Controller for the server.js 
const controller = require('./mainCtrl.js'); 


app.get('/books', controller.GetBooks); 
app.get('/books/:id', controller.GetBook); 
app.get('/mypage/:id', controller.GetMyPage); 

app.delete('/mypage', function(req, res, next) { 
    console.log("req.body", req.body) 
    db.delete_mylist([req.body.book_id], function(err, individual) { 


     if(err) res.status(500).send(err); 
     else { 
      db.get_mypage_book(function(err, all) { 
       if(err) res.status(500).send(err); 
       else res.send(all); 
      }); 
     } 
    }); 
}); 

回答

0

它看起來並不像Axios公司支持在體內與deletehttps://github.com/mzabriskie/axios#request-method-aliases

發送數據雖然它不是在禁HTTP規範,你不應該在DELETE請求中用正文發送任何數據。相反,使用路線參數:

app.delete('/mypage/:bookId', function(req, res, next) {...});