简体   繁体   English

如何将道具从预填充的 React 输入表单传递回 Express?

[英]How to pass props from a pre-filled React input form back to Express?

In my React App, I've created a simple support form using Express & Sendgrid.在我的 React App 中,我使用 Express & Sendgrid 创建了一个简单的支持表单。 The post request works with an input form like this:发布请求与这样的输入表单一起使用:

support.jsx支持.jsx

  <form method="POST" action="http://localhost:4000/support">
    <div>
      <label htmlFor="email">
        <div>Email:</div>
        <input type="text" name="name" required />
      </label>
    </div>

support.js (Express) support.js (Express)

app.post('/support', (req, res) => {
  const { email = '', name = '', message = '' } = req.body

  mailer({ email, name, text: message }).then(() => {
    console.log(`Sent the message "${message}" from <${name}> ${email}.`);
    res.redirect('/#success');
  }).catch((error) => {
    console.log(`Failed to send the message "${message}" from <${name}> ${email} with the error ${error && error.message}`);
    res.redirect('/#error');
  })
})

This works and I am able to send an email when I manually type in the email.这有效,当我手动输入电子邮件时,我可以发送电子邮件。 However, since this app has authenticated users, I am now trying to pass the email address of the current user in the input field as shown in this StackOverflow answer:( How do I programatically fill input field value with React? ).但是,由于此应用程序已对用户进行身份验证,因此我现在尝试在输入字段中传递当前用户的电子邮件地址,如此 StackOverflow 答案所示:( 如何使用 React 以编程方式填充输入字段值? )。

The props for currentUser.email is set in App.js and works in other places throughout the app. currentUser.email的 props 在App.js设置,并在整个应用程序的其他地方工作。

SupportForm.jsx updated SupportForm.jsx更新

      <label htmlFor="email">
        <div className="label-content">Email:</div>
        <input type="email" name={props.currentUser.email} placeholder={props.currentUser.email} disabled />
      </label>

support.js (Express) updated support.js (Express)更新

const sgMail = require('@sendgrid/mail');
const { Router } = require('express');
const { User } = require("../models");
const supportRouter = Router();

  supportRouter.post('/support', async (req, res) => {
    
    try {
    const email = await User.findOne({email: req.body.email});
    const { message = '' } = req.body
    
    sgMail.setApiKey('PROCESS.ENV.SG');
   
    const msg = {
        to: 'me@gmail.com',
        from: email,
        subject: 'Support Request',
        text: message
    }

...// omittedcode ...

  module.exports = {
    supportRouter,
  };

This also works ( {"message": "success"} ), however when I get check the email, it is sent from the same email in to: of const msg resulting in a spoof email.这也有效( {"message": "success"} ),但是当我检查电子邮件时,它从同一电子邮件发送to: of const msg导致欺骗电子邮件。

How do I post the current users' email to the request?如何将当前用户的电子邮件发布到请求中? I'm a bit lost at this point so thanks in advance for pointing me in the right direction.在这一点上我有点迷茫,所以提前感谢你为我指明了正确的方向。

Did you try logging req.body.email to check the problem was back-end related?您是否尝试记录req.body.email以检查问题是否与后端相关? I believe your email variable in your controller is undefined which leads sendgrid to default to using the same address for from and to .我相信您的控制器中的email变量undefined ,这导致 sendgrid 默认使用相同的地址fromto But it's hard to know with so little information.但在如此少的信息下很难知道。

I hope that helps!我希望这有帮助!

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

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