#TypeError: undefined is not iterable (cannot read property Symbol(Symbol.iterator))

1 messages · Page 1 of 1 (latest)

inland ocean
#

I am getting this error in my middleware file auth.ts at line 4 i.e. import getConfig from '../configs/config'.

I am basically exporting a function from my config.ts as below:

config.ts

import dotenv from 'dotenv';
dotenv.config();

export default function () {
  return {
    port: process.env.PORT,
    databaseUrl: process.env.DB_URL,
    jwtAccessSecret: process.env.JWT_ACCESS_SECRET    
  }
}

auth.ts

import { Request, Response, NextFunction } from "express";
import jwt from 'jsonwebtoken';
import users from '../models/users';
import getConfig from '../configs/config'; // Getting error here
import '../types/express/extendRequest';

const { jwtAccessSecret } = getConfig();
.
.
.

I have used the same import statement in my server.ts, db.ts etc. and it runs without any issue.

Can anyone please help me figure out what the issue is?

Please let me know if any more info is required.

Thank you

harsh halo
#

you're trying to iterate over undefined, basically

#

do you have a stacktrace

#

a stacktrace would help a lot

inland ocean
#
import getConfig from '../configs/config';
           ^
TypeError: undefined is not iterable (cannot read property Symbol(Symbol.iterator))
    at /home/ayush/Ayush Jain/MERN/Monolithic/authServer/src/middlewares/auth.ts:35:59
    at Generator.next (<anonymous>)
    at /home/ayush/Ayush Jain/MERN/Monolithic/authServer/src/middlewares/auth.ts:8:71
    at new Promise (<anonymous>)
    at __awaiter (/home/ayush/Ayush Jain/MERN/Monolithic/authServer/src/middlewares/auth.ts:4:12)
    at authenticateToken (/home/ayush/Ayush Jain/MERN/Monolithic/authServer/src/middlewares/auth.ts:34:85)
    at Layer.handle [as handle_request] (/home/ayush/Ayush Jain/MERN/Monolithic/authServer/node_modules/express/lib/router/layer.js:95:5)
    at trim_prefix (/home/ayush/Ayush Jain/MERN/Monolithic/authServer/node_modules/express/lib/router/index.js:328:13)
    at /home/ayush/Ayush Jain/MERN/Monolithic/authServer/node_modules/express/lib/router/index.js:286:9
    at Function.process_params (/home/ayush/Ayush Jain/MERN/Monolithic/authServer/node_modules/express/lib/router/index.js:346:12)
harsh halo
#

what's auth.ts:35

inland ocean
# harsh halo what's `auth.ts:35`
const [bearer, accessTokenHeader, refreshTokenHeader] = req.headers['authorization'] && req.headers['authorization'].split(' ') as any;```
harsh halo
#

so req.headers['authorization'] is undefined

#

you shouldn't be trying to destructure from a possibly nonexistant value, ts wouldve told you that but you used as any lol

#

isn't it usually Authorization?

#

also you can use ?. there rather than doing the a && a.b thing
then provide a default/defaults

#

or you may want to just check for the auth header being there and just branch from that with just an if

inland ocean
#

I am actually fairly new to ts. This is my first project using ts so I am the learning phase. But thanks for the explanation and the links.

harsh halo
#

tip: don't use any or as/<> or ! unless absolutely necessary

inland ocean
#

Yes I read that somewhere but ts wasnt allowing me destructure the array

#

so as a last resort I used any

harsh halo
#

so you disabled ts and now you got a harder-to-trace runtime error

#

don't disregard what ts is saying ¯_(ツ)_/¯

inland ocean
harsh halo
#

ts is

inland ocean
harsh halo
#

!ts

#

bro this doesn't have strictNullChecks on 💀

#

sec

inland ocean
#

Well seems like I got a lot of reading to do 😅

#

Also, one last thing

harsh halo
#

ok seems like this works

#

maybe im just misremembering how to write those examples painpeko

#

!ts

#

goddammit

inland ocean
#

WTH 😂

harsh halo
#

needed a typecast haachama_aaaaaaaaaa

#

!ts

fathom crystalBOT
#
const a = ("" as string).split("");
//    ^? - const a: string[]
// guaranteed string

let b: string | undefined = undefined as string | undefined;
//  ^? - let b: string | undefined
// your case, you don't know if the auth header is actually present
const bResult1 = b.split("");
//               ^
// 'b' is possibly 'undefined'.
//    ^? - const bResult1: string[]
const bResult2 = b && b.split("");
//    ^? - const bResult2: "" | string[] | undefined```
harsh halo
#

there, there we go

#

finally

#

ignore the clutter on b, basically b is what you have

#

bResult2 is what you're doing

#

split does give an array, yes

#

but split is never reached if b is falsy

#

because the logical and will short-circuit, giving you that falsy value

#

and for any falsy value that isn't a string, there's no iterator, including undefined

#

so that's where your error is

inland ocean
#

Ahh it makes so much sense now.

#

Sir you are a great teacher I must say 🫡

harsh halo
#

i am in extreme pain

inland ocean
#

May I ask why?

harsh halo
#

[gestures broadly at everything]

inland ocean
#

Can I add you as a friend if thats okay with you?

inland ocean
harsh halo
#

nah just got a lot going on rn, i really shouldn't be here lol

inland ocean
#

Thanks for your help and prompt response btw, it really saved me a lot of time man. Really appreciate it.

harsh halo
inland ocean
#

I am doing that, just let me get a hang of it and I will get back to you if I am stuck.

#

btw i sent you friend req