0

The string ''+notify_name+' likes your post.' is coming from the database. We are storing this static string in the database.

variable ntfnFound[0].body contains this string that we fetched from the database.

var notify_name = 'tintu';

console.log(ntfnFound[0].body);   /* ''+notify_name+' likes your post.' */

var body = ntfnFound[0].body;

console.log(body);   /* ''+notify_name+' likes your post.' */

console.log(''+notify_name+' likes your post.');   /*tintu likes your post*/

My question is,

In the above code why not 'console.log(body);' displays 'tintu likes your post' ??

or

Why +notify_name+ is not getting replaced ??

Also is there any solution for converting the string in 'var body' to 'tintu likes your post' ??

IN the following code works,

console.log(''+notify_name+' likes your post.');   /*tintu likes your post*/
  • It sounds like you want to store a template in the db and then apply some data to that template when it's rendered? – Shanimal Oct 29 '15 at 14:28

2 Answers2

1

My first guess would be: You saved the STRING in your DB, when pulling it out, it is still a String, so must likely the ' and + are escaped: \' and \+.

What you are trying to accomplish is to save a String in your DB where you later want to insert the value of your variable.

My approach would be: saving something like "{0} likes your post" and then use some kind of printf function on it.

have a look at : JavaScript equivalent to printf/string.format

Community
  • 1
  • 1
null
  • 45
  • 1
  • 5
0

There are no automagic replacements in JS - you'll have to it yourself with body.replace("+notify_name+",notify_name);.

The reason it worked in the log is. because JS did what you told it to do: '' is an empty string, then you catenated the name and the text 'likes your post'.

MBaas
  • 7,248
  • 6
  • 44
  • 61