res.json is not a function

publicado por: Anonymous

Este es mi endpoint

_x000D_

_x000D_

router.post("/users/github/:code", function(req, res) {_x000D_
  var state;_x000D_
  fetch('https://github.com/login/oauth/access_token/', {_x000D_
    method: 'POST',_x000D_
    headers: {_x000D_
      Accept: 'application/json',_x000D_
      'Content-Type': 'application/json'_x000D_
    },_x000D_
    body: JSON.stringify({_x000D_
      client_id: 'xxx',_x000D_
      client_secret: 'xxx',_x000D_
      code: req.params.code_x000D_
    })_x000D_
  }).then(function(res) {_x000D_
    return res.json();_x000D_
  }).then(function(body) {_x000D_
    if (body.error !== 'bad_verification_code') {_x000D_
      const token = createToken(body.access_token);_x000D_
      var access_token = body.access_token;_x000D_
      fetch('https://api.github.com/orgs/xxx/members?access_token=' + access_token, {_x000D_
          method: 'GET',_x000D_
          headers: {_x000D_
            Accept: 'application/json',_x000D_
            'Content-Type': 'application/json'_x000D_
          }_x000D_
        }).then(res => res.json())_x000D_
        .then(res => {_x000D_
_x000D_
          fetch('https://api.github.com/user?access_token=' + access_token, {_x000D_
              method: 'GET',_x000D_
              headers: {_x000D_
                Accept: 'application/json',_x000D_
                'Content-Type': 'application/json'_x000D_
              },_x000D_
            }).then(res => res.json())_x000D_
            .then(user => {_x000D_
_x000D_
              var username = user.login;_x000D_
              var insert;_x000D_
              for (insert = 0; insert < res.length; insert++) {_x000D_
_x000D_
                if (username == res[insert].login) {_x000D_
                  state = true;_x000D_
                }_x000D_
_x000D_
              };_x000D_
              console.log(state)_x000D_
              if (state) {_x000D_
                res.json({_x000D_
                  success: true,_x000D_
                  access_token: body.access_token,_x000D_
                  jwt: token_x000D_
                });_x000D_
              } else {_x000D_
                res.json({_x000D_
                  success: false_x000D_
                });_x000D_
              }_x000D_
            });_x000D_
        });_x000D_
_x000D_
    } else {_x000D_
      res.json({_x000D_
        success: false_x000D_
      });_x000D_
    }_x000D_
_x000D_
  });_x000D_
});

_x000D_

_x000D_

_x000D_

Y esta es la parte que esta tirando el error

if(state){
    res.json({success: true , access_token: body.access_token, jwt: token});
}else{
    res.json({success: false});
}

Porque puede estar ocurriendo esto?

solución

Lo que pasa es que estás llamando res a 2+ mismos argumentos, el de express y el de los objectos fetch que realizas, por lo cual se están redeclarando, debes darle un nombre diferente a cada argumento para que el argumento res de express pueda usar el método json, es decir:

_x000D_

_x000D_

// Este argumento res es el que se encarga de tu res.json(); _x000D_
router.post("/users/github/:code", function(req, res) {_x000D_
  var state;_x000D_
  // Primer fetch _x000D_
  fetch('https://github.com/login/oauth/access_token/', {_x000D_
    method: 'POST',_x000D_
    headers: {_x000D_
      'Accept: application/json', // Aquí te faltaba una comilla al prinicipio_x000D_
      'Content-Type': 'application/json'_x000D_
    },_x000D_
    body: JSON.stringify({_x000D_
      client_id: 'xxx',_x000D_
      client_secret: 'xxx',_x000D_
      code: req.params.code_x000D_
    })_x000D_
  }).then(function(resInFetch) { // Primer res en primer fetch_x000D_
    return res.json();_x000D_
  }).then(function(body) {_x000D_
    if (body.error !== 'bad_verification_code') {_x000D_
      const token = createToken(body.access_token);_x000D_
      var access_token = body.access_token;_x000D_
      // Segundo fetch_x000D_
      fetch('https://api.github.com/orgs/xxx/members?access_token=' + access_token, {_x000D_
          method: 'GET',_x000D_
          headers: {_x000D_
            'Accept: application/json', // Aquí te faltaba una comilla_x000D_
            'Content-Type': 'application/json'_x000D_
          }_x000D_
        }).then(resInSecondFetch => res.json()) // Segundo res en segundo fetch_x000D_
        .then(resInSecondFetch => {_x000D_
          // Tercer fetch_x000D_
          fetch('https://api.github.com/user?access_token=' + access_token, {_x000D_
              method: 'GET',_x000D_
              headers: {_x000D_
                'Accept: application/json', // Aquí te faltaba otra comilla_x000D_
                'Content-Type': 'application/json'_x000D_
              },_x000D_
            }).then(resInThirdFetch => res.json()) // Tercer res en tercer fetch_x000D_
            .then(user => {_x000D_
_x000D_
              var username = user.login;_x000D_
              var insert;_x000D_
              for (insert = 0; insert < res.length; insert++) {_x000D_
_x000D_
                if (username == res[insert].login) {_x000D_
                  state = true;_x000D_
                }_x000D_
_x000D_
              };_x000D_
              console.log(state)_x000D_
              if (state) {_x000D_
                res.json({_x000D_
                  success: true,_x000D_
                  access_token: body.access_token,_x000D_
                  jwt: token_x000D_
                });_x000D_
              } else {_x000D_
                res.json({_x000D_
                  success: false_x000D_
                });_x000D_
              }_x000D_
            });_x000D_
        });_x000D_
_x000D_
    } else {_x000D_
      res.json({_x000D_
        success: false_x000D_
      });_x000D_
    }_x000D_
_x000D_
  });_x000D_
});

_x000D_

_x000D_

_x000D_

Respondido por: Anonymous

Leave a Reply

Your email address will not be published. Required fields are marked *