-1

I have a problem with the template literals JS. I get user data and an SMS template from a MySQL table.

// User fields

let name = data.info.name; // Chingis
let start_datetime = data.info.start_datetime; // 05.07.2020 15:20
let end_time = data.info.end_time; // 16:20

// SMS field (text)

// From the database
// Text: Dear ${name}! Your contract visit has been approved for ${start_datetime} - ${end_time}. We are waiting for you!
sms_template = data.sms.text; 

My insertion

$('#sms-message').text("`" + sms_template + "`");

The result has not changed: Dear ${name}! Your contract visit has been approved for ${start_datetime} - ${end_time}. We are waiting for you!

quote: How to insert variables into a template?

2 Answers2

1

You must use eval() function there to convert that string into template.

so change this:

$('#sms-message').text("`" + sms_template + "`");

into this:

$('#sms-message').text(eval("`" + sms_template + "`"));

reference: Convert a string to a template string

Jovylle
  • 859
  • 7
  • 17
0

Just replace this line

$('#sms-message').text("`" + sms_template + "`");

with

$('#sms-message').text(`${sms_template}`);
Tushar
  • 665
  • 5
  • 11