-3

I have a variable named input which is coming directly from a mySQL database.

GROUP_CONCAT(name,"(",id,")" SEPARATOR ",") AS data

When I test, if it is a string, then the output is "no"

 "render": function (data, type, row) {

    var input = data;

    if (Object.prototype.toString.call(input) == '[object String]') {
      console.log("yes");
    } else {
      console.log("no");
   }
},

But I need the output to be "yes"...

I tested to convert the input input.toString(); But then I do not get any output at all

peace_love
  • 6,229
  • 11
  • 69
  • 157
  • 1
    I edited your code to be in an exectuable snippet. As you can see it works fine. This means that your `input` variable *is not a string*. Are you sure it's not an array of strings? – Rory McCrossan Nov 24 '17 at 09:22
  • When running your code in the console, I get the correct result: https://i.imgur.com/yjNBPrr.png – nbokmans Nov 24 '17 at 09:23
  • @nbokmans Yes of course this code is working. But my variable is coming from a database and is not a string. This is my problem – peace_love Nov 24 '17 at 09:30
  • 1
    @Jarla: How do you expect us to fix code that is working fine? Try posting code that doesn't work. i.e. the actual variable value that you *are* using – musefan Nov 24 '17 at 09:31
  • But is there no way to convert any input into a string? – peace_love Nov 24 '17 at 09:33
  • If you select it correctly you shouldn't need to convert it at all – Rory McCrossan Nov 24 '17 at 09:38

2 Answers2

2

You could use typeof instead:

var input = 'cat(13),dog(12),bird(14)';

if (typeof input === "string") {
   console.log('yes');
} else {
   console.log('no');
}

This question was already discussed here: StackOverflow Question

ssc-hrep3
  • 15,024
  • 7
  • 48
  • 87
-1

You can use String(param) function for converting your variable into a string.

i.e.

String(input);
Vaibhav Bhavsar
  • 432
  • 4
  • 12