Home > database >  Expected token-error when inserting a variable into a string - Javascript
Expected token-error when inserting a variable into a string - Javascript

Time:10-19

   var value = liste[eigenschaft];
   document.getElementById("demo6").innerHTML = value;
   var data = '{"products":[{"ingramPartNumber":"7U0169"}, {"ingramPartNumber":"6XH440"}]}';

I made a successful request for an API from one of our online-distributors. In the var data (as shown above) I can list the name/value-pairs (f.E.: ingramPartNumber and the actual partNumber) and I get back the corresponding data.

However, we want to make the call a bit more dynamic. We implemented a search and we want to have the value of the search (in the example above the var value) as an actual value in var data.

However, if I substitute the product-number with the variable (as shown below), it throws an error (expected token identifier) .. What am I missing?

   var value = liste[eigenschaft];
   document.getElementById("demo6").innerHTML = value;
   var data = '{"products":[{"ingramPartNumber":"7U0169"}, {"ingramPartNumber":"'value'"}]}';

CodePudding user response:

You are doing this

   var data = '{"products":[{"ingramPartNumber":"7U0169"}, {"ingramPartNumber":"'value'"}]}';

Where you should be using interpolation or concatenation

   var data = '{"products":[{"ingramPartNumber":"7U0169"}, {"ingramPartNumber":'   value   '}]}';
  • Related