0

I'm building a web app that uses Angularjs. In one of my controllers I make a call to the backend that returns the current login status of the user.

   $http.get('http://localhost:3000/status')
     .success(function(status) {
       if (status == 'internal') {
         // do stuff...

Even when the requests responds with status equal to 'internal', (status == 'internal') evaluates to false. To test this a bit I put

  $http.get('http://localhost:3000/status')
    .success(function(status) {
      console.log(typeof status)
      console.log(status)
      console.log(status == "internal") 

which prints

    string 
    "internal" 
    false 

Any Ideas about what could be going wring here?

7
  • 1
    what happens when you use 3 equality marks? (status === 'internal') also, can there maybe be some extra whitespace or something like that? Commented Dec 10, 2013 at 18:56
  • 2
    make sure no extra white space being sent...check length of status vs length of your test string Commented Dec 10, 2013 at 18:57
  • Are you returning internal or "internal" (with quotes). I ask because console.log(typeof status) would display as a string but you didn't double quote that in your output. Commented Dec 10, 2013 at 18:59
  • What browser are you using? Commented Dec 10, 2013 at 19:00
  • 1
    BTW...why don't you send json? Commented Dec 10, 2013 at 19:02

2 Answers 2

7

Sorry, this is only a valid answer if you're using Chrome. It looks like Firefox will wrap strings in quotes on the console.


Your string has extra quotes around it. console.log(string) won't show quotes. Observe:

console.log("what")
what 

The only way you're seeing "internal" instead of internal is if you have extra quotes around your string.

Sign up to request clarification or add additional context in comments.

Comments

2

This should be what you're looking for:

console.log(status == '"internal"');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.