Javascript ES6承诺“;待决“;vs";“履行”;

Javascript ES6承诺“;待决“;vs";“履行”;,javascript,es6-promise,Javascript,Es6 Promise,你好。 需要一些新鲜的眼睛。不确定为什么我的承诺在浏览器控制台中返回“待定”: // contact.js (react) /* trim */ submitForm = (event) => { console.log(JSON.stringify(data)) fetch('/contact', { method: 'POST', headers: { 'Accept': 'application/json, text/

你好。 需要一些新鲜的眼睛。不确定为什么我的承诺在浏览器控制台中返回“待定”:

// contact.js (react)
/* trim */
submitForm = (event) => {
    console.log(JSON.stringify(data))
    
    fetch('/contact', {
      method: 'POST',
      headers: {
        'Accept': 'application/json, text/plain, */*',
        'Content-Type': 'application/json'
      },
      body: JSON.stringify(data)
    }).then( (res) => {
        res.status === 200 ? this.setState({ submitted: true }) : ''
        console.log('submitted: ' + this.state.submitted)
        console.log('status:')
        console.log(res.json())
    })
}
/* trim */


// server.js (node, express)
/* trim */
server.post('/contact', (req, res) => {
    const { emailAddress = '', emailName = '', emailMessage = '' } = req.body
    console.log(req.body) // outputs correct json obj 
    
    let mgData = {
        from: emailName + ' <' + emailAddress + '>',
        to: mailgun_from_who,
        subject: 'From CV Site',
        text: emailMessage,
    }
    const mailgun = require('mailgun-js')({ apiKey: mailgun_api_key, domain: mailgun_domain }) 
    
    /* These work independently 

    mailgun.validate(emailAddress, true, function (error, body) {
        console.log(body) // correctly outputs on server console the json obj returned from mailgun
    }
    mailgun.messages().send(mgData, function (error, body) {
        console.log(body) // correctly outputs on server console the json obj returned from mailgun
    }
    */
    
    /* Now want to promise the validate so if email address does not validate, then 
        I don't want to send the message, but rather return some message to the contact form
        on the browser 
        
       Each call to mailgun returns the correct json obj on the server console, 
        but in the browser's console, the 'status' is 'Promise { <state>: "pending" }'
        So, not sure why that's happening in the code below: */
    
    const validateMsg = new Promise( (resolve, reject) => {
        mailgun.validate(emailAddress, true, function(error,body) {
            if(body) { resolve(body) }
            else { reject(Error(error)) }
        }
    })
    
    // validateMsg.then(successCallback, failureCallback)
    validateMsg.then(
        function(result) {
            console.log('here comes the validate result');
            console.log(result); // validate body
            res.send(result) // should send validate body back to browser as the promise becomes fulfilled. 
        }, function(err) {
            console.log('here comes the validate result');
            console.log(err); // error message
            res.send(err) // should send error message back to the browser as the promise
        }
    )
    
    
})

/* trim */
//contact.js(react)
/*修剪*/
submitForm=(事件)=>{
log(JSON.stringify(数据))
获取(“/contact”{
方法:“POST”,
标题:{
“Accept':“application/json,text/plain,*/*”,
“内容类型”:“应用程序/json”
},
正文:JSON.stringify(数据)
})。然后((res)=>{
res.status==200?this.setState({submitted:true}):“”
console.log('submitted:'+this.state.submitted)
console.log('状态:')
console.log(res.json())
})
}
/*修剪*/
//server.js(节点,express)
/*修剪*/
server.post('/contact',(请求、回复)=>{
const{emailAddress='',emailName='',emailMessage=''}=req.body
console.log(req.body)//输出正确的json obj
设mgData={
发件人:emailName+“”,
致:mailgun_来自_who,
主题:“来自简历网站”,
文本:emailMessage,
}
const mailgun=require('mailgun-js')({apiKey:mailgun\u api\u key,domain:mailgun\u domain})
/*它们独立工作
mailgun.validate(emailAddress、true、函数(错误、正文){
log(body)//在服务器控制台上正确输出从mailgun返回的json obj
}
mailgun.messages().send(mgData,函数(错误,正文){
log(body)//在服务器控制台上正确输出从mailgun返回的json obj
}
*/
/*现在要承诺验证,所以如果电子邮件地址不验证,那么
我不想发送消息,而是将一些消息返回到联系人表单中
在浏览器上
每次调用mailgun都会在服务器控制台上返回正确的json obj,
但在浏览器的控制台中,“状态”是“承诺{:“待定”}”
所以,我不知道下面的代码中为什么会出现这种情况:*/
const validateMsg=新承诺((解决、拒绝)=>{
mailgun.validate(emailAddress、true、函数(错误、正文){
if(body){resolve(body)}
else{reject(Error(Error))}
}
})
//validateMsg.then(successCallback,failureCallback)
那么(
功能(结果){
log(“验证结果出来了”);
console.log(结果);//验证正文
res.send(result)//应在承诺履行时将验证主体发送回浏览器。
},函数(err){
log(“验证结果出来了”);
console.log(err);//错误消息
res.send(err)//应将错误消息作为承诺发送回浏览器
}
)
})
/*修剪*/
当我提交表单并查看控制台时,浏览器会立即输出数据 在调用fetch()之前也应该如此 输出数据obj。然后两者都等待mailgun处理验证请求

一旦validate从mailgun返回,浏览器就会输出:

提交:正确

地位:

>承诺{:“待定”}

同时,服务器控制台输出从mailgun返回的json obj

因此,我不确定为什么mailgun返回的json obj没有被发送回浏览器。

res.json()
返回一个承诺,因此您必须执行以下操作:

submitForm = (event) => {
    console.log(JSON.stringify(data))

    fetch('/contact', {
      method: 'POST',
      headers: {
        'Accept': 'application/json, text/plain, */*',
        'Content-Type': 'application/json'
      },
      body: JSON.stringify(data)
    }).then( (res) => {
        res.status === 200 ? this.setState({ submitted: true }) : '';
        console.log('submitted: ' + this.state.submitted)
        return res.json();
    }).then((data)=>{
        console.log('status:')
        console.log(data)
    });
}