Node.js mongodb:FetchAPI POST请求返回一个奇怪的响应

Node.js mongodb:FetchAPI POST请求返回一个奇怪的响应,node.js,mongodb,reactjs,express,fetch-api,Node.js,Mongodb,Reactjs,Express,Fetch Api,我想使用FetchAPI向mongodb发送POST请求。GET请求已使用fetch API成功实现,但POST请求未成功实现 在控制台日志中,POST请求返回奇怪的响应,如下所示: {n: 1, opTime: {…}, electionId: "7fffffff0000000000000001", ok: 1} electionId:"7fffffff0000000000000001" n:1 ok:1 opTime:{ts: "6490526445679411201", t: 1} __p

我想使用FetchAPI向mongodb发送POST请求。GET请求已使用fetch API成功实现,但POST请求未成功实现

在控制台日志中,POST请求返回奇怪的响应,如下所示:

{n: 1, opTime: {…}, electionId: "7fffffff0000000000000001", ok: 1}
electionId:"7fffffff0000000000000001"
n:1
ok:1
opTime:{ts: "6490526445679411201", t: 1}
__proto__:Object
我从来没有遇到过这种反应,我不知道这意味着什么

我的代码如下

server.js

app.post('/recipe', (req, res) => {
console.log(req.body); // this console.log returns empty object : {}

db.collection('recipe').insertOne(req.body, (err, result) => {
   if (err) return console.log(err);
   console.log('saved to database')
   res.json(result);
   });
});
index.js

class App extends Component {
   constructor(props){
   super(props);
   this.state={
        results: [],
        name: '',
        ingredients: '',
        descriptions: '',
        modal: false
     }
    this.handleSubmit = this.handleSubmit.bind(this);
    }

 handleSubmit(e) {
    e.preventDefault();
    const { name, ingredients, descriptions } = this.state;
    fetch('/recipe', {
       method: 'POST',
       body: JSON.stringify({
       name,
       ingredients,
       descriptions
       }),
    headers: { 'Accept': 'application/json, text/plain, */*',
   'Content-Type': 'application/json'}
    })
   .then( res => res.json())
   .then( res => console.log(res)); //this console.log returns strange response
   }   

 render() {

 const { name, ingredients, descriptions } = this.state;
 return (
   <div className="App"> 
    <h1>Recipe List</h1> 
    <form onSubmit={this.handleSubmit}>
    <input
        value={name}
        type="text"
        onChange={e => this.setState({ name: e.target.value })}
        placeholder="name" />
      <input
        value={ingredients}
        type="text"
        onChange={e => this.setState({ ingredients: e.target.value })}                
        placeholder="ingredients" />
      <input
        value={descriptions}
        type="text"
        onChange={e => this.setState({ descriptions: e.target.value })}
        placeholder="descriptions" />

         <input type="submit" onClick={this.toggle}/>
       </form>
    </div>           
    )
   }
  }

  export default App;
类应用程序扩展组件{
建造师(道具){
超级(道具);
这个州={
结果:[],
名称:“”,
成分:'',
说明:“”,
模态:假
}
this.handleSubmit=this.handleSubmit.bind(this);
}
handleSubmit(e){
e、 预防默认值();
const{name,components,description}=this.state;
获取(“/recipe”{
方法:“POST”,
正文:JSON.stringify({
名称
成分,
描述
}),
标题:{'Accept':'application/json,text/plain,*/*',
“内容类型”:“应用程序/json”}
})
.then(res=>res.json())
.then(res=>console.log(res));//此console.log返回奇怪的响应
}   
render(){
const{name,components,description}=this.state;
返回(
菜谱
this.setState({name:e.target.value})}
占位符=“名称”/>
this.setState({components:e.target.value})
占位符=“成分”/>
this.setState({descriptions:e.target.value})}
占位符=“说明”/>
)
}
}
导出默认应用程序;

提前感谢您的帮助,

mongoDBinsertOne返回成功与否的信息。 如果要获取插入的数据,请尝试此操作

db.collection('recipe').insertOne(req.body,(err, result)=>{
    if (err) return console.log(err);
    console.log('saved to database');
    res.json(result.ops[0]);
});

您是否尝试将
标题添加到您请求的url之后传递的对象中?对不起,什么是p.o@我很抱歉。编辑问题没关系,你没问题。许多与问题相关的获取API都是“header”,所以我在阅读了stackoverflow中的其他答案后添加了这个API,尽管它不起作用。谢谢。现在console.log返回带有_-id的对象。
{u-id:“5a1309995f82754b4c4baaa4”}\u-id:“5a1309995f82754b4c4baaa4”uuu-proto:object
我想知道的是为什么它不包含身体数据@godsenalmongodb api不提供插入的文档。您已经在req.body中获得了数据!如果需要,您只需添加_id。或者你可以用猫鼬代替。我在邮递员和mLab中都签了名,它是空的,只有id:
{“\u id:“5a130d53fdbaa54b8a22af85”}
终端中的
请求主体
是空的<代码>侦听保存到数据库的8080{}
So。。这不是mongodb的问题。使用主体解析器?