我正在尝试获取请求,但仍无法正常工作。这是我的工作
客户端:
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>
)
}
email-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
答案 0 :(得分:0)
可能只是需要将handeSendRequest声明为异步函数:
{{1}}
答案 1 :(得分:0)
await只能在异步函数中调用,请尝试以下操作。
async handeSendRequest() {
await fetch...
}
答案 2 :(得分:0)
await仅在异步功能中起作用。您可以将获取请求更改为以下内容:
async handeSendRequest(){
let response= await fetch('/api/sendIt', {method:'post', headers: {'Content-type' : 'application/json' }}, body: JSON.stringify(this.state));
let result=await response.json();
console.log(result)
}