0

I am trying to build a webhook for dialogue flow but cannot access some variable in code when i run code match comes out to be undefined

var findclass = (data, day, time) => {
      var match;
      data.forEach(entry => {
        if (entry.day === day && entry.time === time) {
          console.log("found");
          match=entry;//this statement has no effect on above var match
//instead it creates a new local variable
        }
      });
      return match;
    }



exports.tt = functions.https.onRequest((request, response) => {
  let qr = request.body.queryResult;
  let day = qr.parameters.day;
  let time = parseInt(qr.parameters.time.substring(11, 13));
  let data = [
    {
      day: "monday",
      time: 11,
      type: "lecture",
      batches: ["A", "B1", "B4", "B3", "B2"],
      subject: {
        name: "Basic Numerical Methods",
        name_short: "BNM",
        coursecode: "17B1NMA531",
        coursecode_short: "17MA531"
      },
      class: "CS1",
      teachers: "SSH",
      semester: 5
    },
    {
      day: "monday",
      time: 15,
      type: "lecture",
      batches: ["A6", "A9"],
      subject: {
        name: "Environmental Science",
        name_short: "EVS",
        coursecode: "15B11GE301",
        coursecode_short: "GE301"
      },
      class: "CS1",
      teachers: "EKT",
      semester: 5
    }]
var match = findclass(data, day, time);
  console.log(JSON.stringify(match));
  if (match) {
    response.send({
      fulfillmentText: `Yes, ${match.subject.name_short || match.subject.name}`
    });
  } else {
    response.send({
      fulfillmentText: `No class on ${day} ,${time} hr`
    });
  }

also vscode code is showing var match is unused if i remove return match statement,this means it is not considering match = entry , but i cannot understand why ?

  • The VS Code "unused variable" means, that the value is never read. – Teemu Oct 24 '19 at 17:44
  • I mean the variable never gets assigned any value. So function returns undefined always . even if console.log prints "found" . – Tanishq Manuja Oct 24 '19 at 17:46
  • Does the code work if you switch it to a `for` loop instead? Since `forEach` loops create a function, the scopes might be different. Also, `forEach` loops are slower. – Ahndwoo Oct 24 '19 at 19:24
  • No, I tried for loop and also every instead of foreach.The problem always remains same , Inside the loop it creates a new variable of match but does not use which is already declared. – Tanishq Manuja Oct 25 '19 at 03:27

3 Answers3

1

I don't know the reason but two changes fixed the code

1) Adding "use strict" at the top.
2) defining function as

const findclass = () => {}

instead of var

0

My guess is that your declaration of:

var match ...

is being hoisted ... see:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/var

In your code you have declared:

var match ...

twice. I think the declaration in the body of the findClass function might want to be changed to a local variable ...

let match;

(This is a guess, will delete this answer on request or if better answer comes along).

Kolban
  • 13,794
  • 3
  • 38
  • 60
0

My guess is that nothing is actually matching in the loop, and therefore match remains undefined. Have you made sure something is actually matching in the loop?

Also, in VSCode, if you are just assigning a variable, but not using it (e.g., in a return statement), it will show the variable as being unused, so that is expected.