【发布时间】:2019-03-12 09:26:34
【问题描述】:
我正在尝试发出 fetch 请求但无法正常工作。这是我的工作
客户端:
this.state= {
from: "from@email.com",
to: "to@email.com",
subject: "mainSubject",
message: "main message",
}
handeSendRequest(){
await fetch('/api/sendIt', {method:'post', headers: {'Content-type' : 'application/json' }}, body: JSON.stringify(this.state)){
}
}
render(){
return(
<Button onClick={this.handeSendRequest} > Send it </Button>
)
}
电子邮件-util.js
const mailgun = require('mailgun-js')(mailGunConfig);
var data = {
from: req.body.from,
to: req.body.to,
subject: req.body.subject,
text: req,body.text
}
mailgun.message().send(data,function(error, body){
if(error){
console.log(error)
} else {
console.log(body)
}
} )
server.js
const app = express();
const emailUtil = require('./email-util');
app.use('/api/sendIt', emailUtil);
我是前端和后端工作的新手,并且一直在处理这个问题。我得到的错误是await is a reserved word
【问题讨论】:
-
你的 email-util.js 需要是一个模块化的路由处理程序。检查底部here 的express.Router 部分。您需要
router.post('/', ...),因为您要发送 POST 请求。 (并暂时删除await,因为无论如何您在发送请求后什么都不做) -
您必须将您的函数标记为
async才能使用await。尾随花括号也是语法错误。
标签: javascript node.js reactjs