Getting JavaScript runtime error: "ReferenceError: "line" is not defined. (JS-Person.js)

Not sure why I got reference error on this code, but please help 🙏

 

 

var response = context.getVariable("resp.orgs") || context.getVariable("req.orgs");
var org = [];

function createOrg() {
	var parsedResponse = JSON.parse(response);
	var orgs =
		parsedResponse.abs && parsedResponse.abs['abs'] ?
		parsedResponse.cdr['cdr'] :
		parsedResponse.test && parsedResponse.test['test'] || [];
	
	newLine = lines.map((line) => {
		return {
			"person": line.person.value : {"#attrs":{"@nil":true}},
			"email": line.email
		};
	});
	
	return org;
}

 

Thank you!

Solved Solved
1 1 212
1 ACCEPTED SOLUTION

The dash is not a valid character within identifier for a JS name (cite). You cannot write parsedResponse.res-lines to resolve a field named 'res-lines' in the JS object, because res-lines is not a JS identifier. In JavaScript, identifiers are (commonly) made of alphanumeric characters, underscores (_), and dollar signs ($), with the caveat that an identifier must not start with a numeric character.

Here is the snip in your code I'm referring to:

 


var lines =
    parsedResponse.res-lines && parsedResponse.res-lines['res-line'] ?
    parsedResponse.res-lines['res-line'] :
    parsedResponse.op-lines && parsedResponse.op-lines['op-line'] || [];

 

If I run your code through a JS prettifier, here's what I get:

 

var lines =
  parsedResponse.res - lines && parsedResponse.res - lines["res-line"]
    ? parsedResponse.res - lines["res-line"]
    : (parsedResponse.op - lines && parsedResponse.op - lines["op-line"]) || [];

 

Do you see the difference? parsedResponse.res-lines is parsed by JS into parsedResponse.res - lines, in other words an arithmetic expression subtracting the value lines from the value of parsedResponse.res . I don't think that is what you intend. You can use bracket notation if you need a property value that contains a dash. So:

 

// WRONG
var lines =
    parsedResponse.res-lines && parsedResponse.res-lines['res-line'] ?
    parsedResponse.res-lines['res-line'] :
    parsedResponse.op-lines && parsedResponse.op-lines['op-line'] || [];

// RIGHT, maybe?
var lines =
  parsedResponse['res-lines'] && parsedResponse['res-lines']["res-line"]
    ? parsedResponse['res-lines']["res-line"]
    : (parsedResponse['op-lines'] && parsedResponse['op-lines']["op-line"]) || [];

 

But I think the latter can be clarified. In that single expression, you mixed a ternary with a chained logical OR.  I would just stick with one or the other. The following seems clear.

var lines =
  (parsedResponse['res-lines'] && parsedResponse['res-lines']["res-line"]) ||
  (parsedResponse['op-lines'] && parsedResponse['op-lines']["op-line"]) || 
  [];

 

View solution in original post

1 REPLY 1

The dash is not a valid character within identifier for a JS name (cite). You cannot write parsedResponse.res-lines to resolve a field named 'res-lines' in the JS object, because res-lines is not a JS identifier. In JavaScript, identifiers are (commonly) made of alphanumeric characters, underscores (_), and dollar signs ($), with the caveat that an identifier must not start with a numeric character.

Here is the snip in your code I'm referring to:

 


var lines =
    parsedResponse.res-lines && parsedResponse.res-lines['res-line'] ?
    parsedResponse.res-lines['res-line'] :
    parsedResponse.op-lines && parsedResponse.op-lines['op-line'] || [];

 

If I run your code through a JS prettifier, here's what I get:

 

var lines =
  parsedResponse.res - lines && parsedResponse.res - lines["res-line"]
    ? parsedResponse.res - lines["res-line"]
    : (parsedResponse.op - lines && parsedResponse.op - lines["op-line"]) || [];

 

Do you see the difference? parsedResponse.res-lines is parsed by JS into parsedResponse.res - lines, in other words an arithmetic expression subtracting the value lines from the value of parsedResponse.res . I don't think that is what you intend. You can use bracket notation if you need a property value that contains a dash. So:

 

// WRONG
var lines =
    parsedResponse.res-lines && parsedResponse.res-lines['res-line'] ?
    parsedResponse.res-lines['res-line'] :
    parsedResponse.op-lines && parsedResponse.op-lines['op-line'] || [];

// RIGHT, maybe?
var lines =
  parsedResponse['res-lines'] && parsedResponse['res-lines']["res-line"]
    ? parsedResponse['res-lines']["res-line"]
    : (parsedResponse['op-lines'] && parsedResponse['op-lines']["op-line"]) || [];

 

But I think the latter can be clarified. In that single expression, you mixed a ternary with a chained logical OR.  I would just stick with one or the other. The following seems clear.

var lines =
  (parsedResponse['res-lines'] && parsedResponse['res-lines']["res-line"]) ||
  (parsedResponse['op-lines'] && parsedResponse['op-lines']["op-line"]) || 
  [];