我在发送邮件请求时遇到问题

//表格是模型所必需的​

router.route('/').post(function(req,res,next){
 res.send(req.body)
 form.create(
  {"first_name": req.body.first_name,
  "last_name": req.body.last_name
 })
  .then(function(data){ 
  res.send(data);
  console.log(data);
 }).catch(function(err){console.log(err)});
});

但我需要从客户那里发出,而不是postman .在这里我迷路了.我可以用它,但当我添加Submit action时,它就不起作用了.我需要使用新功能启动另一个东西,而无需重定向到另一个页面.如何通过这个.裁判.名字.值转换为body,以便我可以使用fetch函数??

添加了这个JavaScript/JSON片段

export default class Form extends React.Component {
 constructor(props){
  super(props);
  this.handleSubmit = this.handleSubmit.bind(this);
 }
 handleSubmit(event){ 
  event.preventDefault();
  console.log(this.refs.first_name.value);
  fetch('/', {
   method: 'post',
   body: {
    "first_name": this.refs.first_name.value
   }
  });
 };
 render () {
  return (
   
   <div id="signup">
    <form onSubmit={this.handleSubmit}>
        <input ref="first_name" placeholder="First Name" type="text" name="first_name"/><br />
        <input placeholder="Last Name" type="text" name="last_name"/><br />
       <button type="Submit">Start</button>
    </form>
​
   </div>
​
  )
 }
}

推荐答案

我猜你使用ref的方式已经被弃用了.试试下面,看看你有没有运气.

export default class Form extends React.Component {
 constructor(props){
  super(props);
  this.handleSubmit = this.handleSubmit.bind(this);
 }

 handleSubmit(event){ 
  event.preventDefault();
  fetch('/', {
   method: 'post',
   headers: {'Content-Type':'application/json'},
   body: {
    "first_name": this.firstName.value
   }
  });
 };

 render () {
  return (
   
   <div id="signup">
    <form onSubmit={this.handleSubmit}>
        <input ref={(ref) => {this.firstName = ref}} placeholder="First Name" type="text" name="first_name"/><br />
        <input ref={(ref) => {this.lastName = ref}} placeholder="Last Name" type="text" name="last_name"/><br />
       <button type="Submit">Start</button>
    </form>
​
   </div>
​
  )
 }
}

下面是一个linkrefs的例子

Mongodb相关问答推荐

在单个mongo文档中组合数组与聚合

在MongoDB中获取所有帖子时如何获取相关用户信息

使用mongo'db.Decode(&dto)映射一个嵌套的 struct

更新值导致错误 Golang MongoDB

如何更新mongo中列表最后一个对象的属性

程序可以运行,但我不断收到发送到客户端后无法设置标题,我应该忽略它吗?

查找对象是否在预保存钩子mongoose中更改

Mongo查询子文档的多个字段

在 mongoDB 中存储 java 8 LocalDate

mongodb - 查找具有最接近整数值的文档

NodeJS中的密码重置

如何在mongoose中加入两个集合

MongoDB GridFS VS 直接磁盘 IO

Mongo:无法在 src/mongo/shell/mongo.js:145 连接到服务器 127.0.0.1:27017

初创公司应该考虑哪些数据库系统?

MongoDB:查询和检索嵌入式数组中的对象?

brew install mongodb 错误:Cowardly refusing to `sudo brew install' Mac OSX Lion

试图从 mongoose 获取Collection 列表

mongodb安装失败运行mongod

在 MongoDB 中存储Relationships关系?