axios.post 好像不行?

axios.post doesn't seem to work?

也许我只是没有正确使用 axios,但我目前有一个反应前端和 node.js 后端。

我正在尝试 POST 到我的 api 端点“/api/:id/addItem”,但是在发出请求时没有任何记录。

这是我的代码:

ListForm 组件 ->

import React from 'react';
import * as helpers from '../helpers';

class ListForm extends React.Component {
  state = {
    value: ''
  }

  handSubmit = e => {
    e.preventDefault();
    helpers.addItem(this.props.currentUser.googleId, this.state.value);

    this.setState({value: ''});
  }

  render() {
    return (
      <form onSubmit={this.handleSubmit}>
        <input type="text"
          value={this.state.value}
          onChange={e => this.setState({value: e.target.value})}
        />
        <button>Add item</button>
      </form>
    );
  }
}

export default ListForm;

路线 ->

const mongoose = require('mongoose');
const User = require('../models/userSchema');

module.exports = (app) => {
  app.post('/api/:id/addItem', (req, res) => {
    console.log('HEY!');
  });
};

helpers.js ->

import axios from 'axios';

export const fetchUser = async () => {
  const resp = await axios.get('/api/current_user');

  return resp.data;
}

export const addItem = async (id, newItem) => {
  const resp = await axios.post("/api/" + id + "/addItem", newItem);

  return resp.data;
}

Package.json 显示转发请求->

{
  "name": "client",
  "version": "0.1.0",
  "private": true,
  "proxy": {
    "/auth/google": {
      "target": "http://localhost:5000"
    },
    "/api/*": {
      "target": "http://localhost:5000"
    }
  },
  "dependencies": {
    "axios": "^0.17.1",
    "react": "^16.2.0",
    "react-dom": "^16.2.0",
    "react-scripts": "1.0.17"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test --env=jsdom",
    "eject": "react-scripts eject"
  }
}

这里的问题是 newItem,它不是 json,它只是简单的 value

axios.post("/api/" + id + "/addItem", newItem);

应该是这样的:

axios.post("/api/" + id + "/addItem", {value : newItem});

或从 addItem 传递 json :

helpers.addItem(this.props.currentUser.googleId, this_should_be_json );