0

I try to do iteration on object on nodejs using for in, on my local it's working just fine. but when i deploy it to web service, it pass an error.

but when i tried on my local it work just fine

const queue = {
    mobilize : 'test',
    mobileTurnOn: 'mobileTurnOn',
    pushAdminNotif: 'pushAdminNotif',
    diffLocationAlert: 'diffLocationAlert'
  };
  for ( string in queue) {
    console.log(string, "key")
    console.log(queue[string], "value")
  };

it said variable is not defined

ReferenceError: string is not defined 13 at /app/dist/app.js:59:10 12 at /app/node_modules/amqplib/callback_api.js:15:23 11 at /app/node_modules/amqplib/lib/connect.js:154:9 10 at succeed (/app/node_modules/amqplib/lib/connection.js:283:5) 9 at onOpenOk (/app/node_modules/amqplib/lib/connection.js:262:5) 8 at /app/node_modules/amqplib/lib/connection.js:165:32 7 at /app/node_modules/amqplib/lib/connection.js:159:12 6 at Socket.recv (/app/node_modules/amqplib/lib/connection.js:507:12) 5 at Object.onceWrapper (events.js:420:28) 4 at Socket.emit (events.js:314:20) 3 at Socket.EventEmitter.emit (domain.js:483:12) 2 at emitReadable_ (_stream_readable.js:557:12) 1 at processTicksAndRejections (internal/process/task_queues.js:83:21)

here is my terminal on my local running

1
  • should be for (let string in queue), you have to declare it Commented Jul 28, 2023 at 17:15

1 Answer 1

2

You can try this:

for (const string in queue) {
  console.log(string, "key")
  console.log(queue[string], "value")
};
Sign up to request clarification or add additional context in comments.

2 Comments

oh my god, can't believe i miss to declare it. so upset with my self haha but it leads me to another questions. why on my local it work just fine ?
Some js engines, such as V8, will implicitly declare a global variable if you use for in loop without declaring variable(not strict mode). so I think deployed server may be using strict mode. so you need to declare local variable. - @MuzaniPamasarsa

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.