简体   繁体   English

POST 请求适用于 Postman 但不适用于 axios.post()

[英]POST request works with Postman but not with axios.post()

I created a Note app in React-JS and a REST-api using node and I am running both the front-end and the server concurrently.我在React-JS中创建了一个 Note 应用程序,并使用节点创建了一个REST-api ,并且我同时运行前端和服务器。 This is my REST-Api (I am using mongoDB to store the data)这是我的 REST-Api(我使用 mongoDB 来存储数据)

app.post('/api/post',(req,res)=>{
let title = req.body.title
let content = req.body.content
console.log(title)
console.log(content)
const newNote = new Note ({
        title: title,
        content: content
    })
newNote.save()})

This is my axios.post in my App.jsx file这是我的 App.jsx 文件中的 axios.post

function addItem(item){
axios.post("http://localhost:4000/api/post",{
  title:item.title,
  content:item.content
})
.then(res =>{
  console.log(res)
})
.catch(error=>{
  console.log(error)
})}

The POST request works perfectly via Postman and my data gets stored. POST 请求通过 Postman 完美运行,并且我的数据被存储。 However for the axios.post function it does send a post request back to my api, but when I console.log(title) and console.log(content) the console returns undefined and an empty object gets sent back to my Note collection in my mongoDB database.I have tried finding the problem online but have not been able to find the solution. However for the axios.post function it does send a post request back to my api, but when I console.log(title) and console.log(content) the console returns undefined and an empty object gets sent back to my Note collection in我的 mongoDB 数据库。我已尝试在线查找问题,但无法找到解决方案。 Any help would be much appreciated.任何帮助将非常感激。

This is my entire express rest API code这是我的整个快递 rest API 代码

 const express = require("express"); const app = express(); const mongoose = require("mongoose") const cors = require("cors") app.use(express.static('public')) app.use(express.urlencoded({extended:true})) //this line is to parse any information from forms app.use(cors()) app.set('view engine', 'ejs') const PORT = 4000 main().catch(err => console.log(err)); async function main() { await mongoose.connect('mongodb://localhost:27017/keeperDB'); } const notesSchema = new mongoose.Schema( {title:String, content:String} ) const Note = mongoose.model("Note",notesSchema) app.get("/api", function(req, res){ Note.find(function(err,results){ if (err){ console.log(err) }else{ res.json(results) } }) }); app.post('/api/post',(req,res)=>{ let title = req.body.title let content = req.body.content console.log(title) console.log(content) const newNote = new Note ({ title: title, content: content }) newNote.save() }) app.listen(PORT, function(){ console.log(`Server started on port ${PORT}.`); });

` `

Add app.use(express.json()) middleware to you're code otherwise express server can't extract JSON data from requestapp.use(express.json())中间件添加到您的代码中,否则快递服务器无法从请求中提取 JSON 数据

It seems Your request object is not a JSON object and you need to use app.use(express.json()) .看来您的请求 object 不是JSON object 并且您需要使用app.use(express.json())

Did you use body-parser middleware for node API?您是否为节点 API 使用了 body-parser 中间件? if not take a look at this link Here and use that middleware如果不看看这个链接在这里并使用那个中间件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM