1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

How to make a string variable

Discussion in 'JavaScript' started by ketting00, Jan 12, 2020.

  1. #1
    Hi guys,
    SEMrush
    I've pulled data from a database with a callback function, but the data sent back is a string something like this:
    John Doe
    Code (markup):
    And I've got this error:
     Uncaught ReferenceError: John Doe is not defined at...
    Code (markup):
    So according to this link: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Not_defined

    It says that the result is a variable. So I've to declare a variable, but how to do that? I've been thinking of some thing like this and it isn't work:
    var backData = "var backdata = '" + result[0].name + "'";
    Code (markup):
    Thank you,
     
    Last edited: Jan 12, 2020
    ketting00, Jan 12, 2020 IP
    SEMrush
  2. hdewantara

    hdewantara Well-Known Member

    Messages:
    465
    Likes Received:
    40
    Best Answers:
    21
    Trophy Points:
    130
    #2
    String concatenation in PHP is not with "+" but with "." operator, so perhaps that should be like following?
    $backData = "var backdata = '" . result[0].name . "'";
    PHP:
     
    Last edited: Jan 12, 2020
    hdewantara, Jan 12, 2020 IP
  3. ketting00

    ketting00 Well-Known Member

    Messages:
    726
    Likes Received:
    20
    Best Answers:
    3
    Trophy Points:
    118
    #3
    I did this in JavaScript. It's a node.js server. I ditched PHP some times ago.
     
    ketting00, Jan 12, 2020 IP
  4. ketting00

    ketting00 Well-Known Member

    Messages:
    726
    Likes Received:
    20
    Best Answers:
    3
    Trophy Points:
    118
    #4
    Finally, I got it done:
    var backData = "var backdata = '" + result[0].name + "';";
    Code (markup):
    Just one semicolon can ruin your day.
     
    ketting00, Jan 12, 2020 IP
    JEET, hdewantara and qwikad.com like this.
  5. hdewantara

    hdewantara Well-Known Member

    Messages:
    465
    Likes Received:
    40
    Best Answers:
    21
    Trophy Points:
    130
    #5
    Aw, didn't think of that; I'm still reluctant to follow node.js development. I will, someday.
    Anyways, it's good to know a valid piece of it from you :)
     
    hdewantara, Jan 12, 2020 IP
    JEET likes this.
  6. ketting00

    ketting00 Well-Known Member

    Messages:
    726
    Likes Received:
    20
    Best Answers:
    3
    Trophy Points:
    118
    #6
    You should. So when I get struck I'll have someone helped me ;)

    Beside from that, it's the only platform I'm exciting about.
     
    ketting00, Jan 12, 2020 IP
    JEET likes this.
  7. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,147
    Likes Received:
    1,702
    Best Answers:
    239
    Trophy Points:
    515
    #7
    Just a warning, single quotes are technically invalid in JSON, so depending on how you're dealing with this you'd want to consider:

    
    var backData = 'var backdata = "' + result[0].name + '";';
    
    Code (markup):
    One of the biggest reasons I favor using single-quotes in my codebases, so many things want double quotes in the output.

    That said, I'm wondering why you'd be building client-side code server-side. Doesn't seem kosher.
     
    deathshadow, Jan 12, 2020 IP
  8. ketting00

    ketting00 Well-Known Member

    Messages:
    726
    Likes Received:
    20
    Best Answers:
    3
    Trophy Points:
    118
    #8
    Thank you @deathshadow, that would be helpful.

    I use node.js since it came out. Happens to love the platform.

    I work mostly with real time data and streaming. In the old days these can be possible only with opencv and java applet. When node.js came out you don't need three or four platforms to do the job. Just two. (I never code in Flash.) Now it can be done with only one platform. Data is scant though. You have to try and error to slowly crawl forward. Maybe C or C# can be better. But I know no C.

    People love exploring right?
     
    ketting00, Jan 12, 2020 IP