Andrew Butler
Andrew Butler

Reputation: 1060

Why Node doesn't properly escape string?

I am new to Node, so forgive me if I am asking a dumb question, but I have a string:

var myString = 'DOMAIN\\username';

and I want to use it in an object as such:

var myObject = {
    owner: myString
};

So if I do a console.log(myString) it shows 'DOMAIN\username', but then when I use it in the object it seems that it doesn't escape. The output of myObject would be:

{ owner: 'DOMAIN\\username }

I tried double escaping it and possible converting it to special characters too, but that didn't work. Anyone know what I need to do?

EDIT

The problem is that I have to use this in a SOAP call, so it's giving an error that states that 'DOMAIN\username' does not exist. I don't really need to console log it, I was just trying to see how the arguments were being formatted before I would send the call. I tried JSON.stringify(myObject) as well and that didn't work either. It is still being transferred as 'DOMAIN\username'

Upvotes: 2

Views: 882

Answers (1)

T.J. Crowder
T.J. Crowder

Reputation: 1074285

The output of myObject would be:

{ owner: 'DOMAIN\\username }

That's because you're logging it as an object, e.g. via console.log or similar, and so it's showing you something source-like for it.

Your string correctly has a single literal backslash in it, both in the myString variable and the myObject.owner property. The issue is purely how you're outputting your object.

Upvotes: 1

Related Questions