Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeError: Cannot read property 'length' of undefined #30

Open
devopsadmin-james opened this issue Sep 20, 2020 · 2 comments
Open

TypeError: Cannot read property 'length' of undefined #30

devopsadmin-james opened this issue Sep 20, 2020 · 2 comments

Comments

@devopsadmin-james
Copy link

Hi there,

i have tried to implement oauth2 using this repo.

Everything went well till run node application with below command

node app.js

But when we hit browser below command
http://127.0.0.1:3000/authorize?response_type=code&scope=email%20address&client_id=wHsY4YI6K7gn2bYhsgPv4HB8Sw9bZ0JV
(client_id=wHsY4YI6K7gn2bYhsgPv4HB8Sw9bZ0JV : generated while creating client application)

we get this error: TypeError: Cannot read property 'length' of undefined

i have even tried this url in browser as well

http://127.0.0.1:3000/authorize?response_type=code&scope=myemail@gmail.com&client_id=wHsY4YI6K7gn2bYhsgPv4HB8Sw9bZ0JV

gettting same error.

Check below stack-trace

osboxes@osboxes:~/kong-oauth2-hello-world$ node app.js 
PROVISION_KEY is xE4QYq1PiW6L5645Y9u52xiP5Qu3jLUR
SERVICE_HOST is mockbin.org
KONG_ADMIN is http://127.0.0.1:8001
KONG_API is https://127.0.0.1:8443
API_PATH is /mock
SCOPES is {   "email": "Grant permissions to read your email address",   "address": "Grant permissions to read your address information",   "phone": "Grant permissions to read your mobile phone number" }
Running at Port 3000
/home/osboxes/kong-oauth2-hello-world/app.js:70
      if (json_response.data.length == 1) {
                             ^

TypeError: Cannot read property 'length' of undefined
    at Request._callback (/home/osboxes/kong-oauth2-hello-world/app.js:70:30)
    at Request.self.callback (/home/osboxes/kong-oauth2-hello-world/node_modules/request/request.js:197:22)
    at emitTwo (events.js:126:13)
    at Request.emit (events.js:214:7)
    at Request.<anonymous> (/home/osboxes/kong-oauth2-hello-world/node_modules/request/request.js:1050:14)
    at emitOne (events.js:121:20)
    at Request.emit (events.js:211:7)
    at IncomingMessage.<anonymous> (/home/osboxes/kong-oauth2-hello-world/node_modules/request/request.js:996:12)
    at emitNone (events.js:111:20)
    at IncomingMessage.emit (events.js:208:7)

How to resolve this error

Thanks & regards

@kele1997
Copy link

I met the same issue.
To sovle this, you need to know two things:

  1. this app will send request to kong admin api, but this request is not send from the app's ip, but your browser's ip, so make sure your browser can send request to kong admin api successfully.
  2. maybe you use the lastest kong , and this app seems does't update in time. so you can modify it by your self.

before modify

if (json_response.data.length == 1) {
application_name = json_response.data[0].name;
}
}
callback(application_name);
});
}

after modify

   if (json_response.data.length == 1) { 
+        console.log(json_response); // print json_response, if json_response =='{"message":"not found"}', it seems your browser can't send request to kong admin api
+        application_name = json_response[name];
-        application_name = json_response.data[0].name; 
       } 
     } 
     callback(application_name); 
   }); 
 } 

@josepereira1
Copy link

you should check first if json_response.data is not undefined, before invoke length, without this, server can fail:
if (json_response.data && json_response.data.length == 1) { application_name = json_response.data[0].name; }

josepereira1 added a commit to josepereira1/kong-oauth2-hello-world that referenced this issue Feb 20, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants