首页 > 解决方案 > 使用未定义的 originalUrl 双重获取到 nodejs 服务器

问题描述

我正在获取对 nodeJS 后端的请求,以通过 mssql 库从 sql express 服务器查询数据。我通过 fetch 向服务器发出 2 个请求,首先是用户,然后是第一个承诺的 THEN 子句中的组,它工作正常。

问题是当 THEN 子句执行时,nodeJS 后端收到一个新请求(第三个),req.originalURL 的值为 /undefined。

如何避免对 nodeJS 的第三次调用?

function readGroups(){        
    const comando=`/grupos?id=1`
    var grupos=fetch(comando);      // generates a request.  
                                    // and nodeJS sends data back. Thats Ok

    grupos.then(datos=>{   // But when I press F11 here it generates a new 
                           // request to nodeJS with the req.originalURL 
                           // with the value : /undefined 

         return datos.json().then(data=>{return data;});
    }).then(datosJson=>{
        crearListaGrupos(datosJson); 
    })
}


// Server side
app.get("*",(req,res)=>{
    console.log(req.originalUrl);   // it's /undefined
});

标签: javascriptnode.jsasynchronouspromiseasync-await

解决方案


推荐阅读