Replace .toString () single quotes with double quotes

Perhaps a weird query, but I'm using CouchDB views, which requires the string to be surrounded by double quotes.

It works:

? Key = ["test", "234"]

This will not work:

? Key = ['test', '234']

So, in my NodeJS application, I am trying to create the correct key to go to CouchDB

var key1 = "test"; var key2 = "234"; { key: [key1, key2] } 

It always appears as

 { key: [ 'test', '234' ] } 

Is there an effective way to get my desired result? (double quotes)

+5
source share
4 answers

CouchDB does not require double quotes as such; it needs JSON-encoded parameters.

This is good news for you! Just create your key as you like in JavaScript:

 var key = [ 'test', '234' ] 

And then JSON-encode before sending to CouchDB:

 key = JSON.stringify(key) // Result: the string '["test","234"]' 
+5
source

It is unclear whether you want to pass an object using keys or a string representing the object. If you pass a string, why don't you use JSON.stringify, which uses double quotes?

 var key1 = "test"; var key2 = "234"; JSON.stringify({key: [key1, keys]}) 

This will be useful: {"key": ["test", "234"]}

+1
source

I have not tested this, but I think you could use String.fromCharCode (34) for double quotes instead of typing them directly. So

var key1 = String.fromCharCode (34) + test + String.fromCharCode (34);

Or even

var doubleQuote = String.fromCharCode (34); var key1 = doubleQuote + test + doubleQuote;

0
source

You can try this in your node.js application

 var key1 = '"test"'; var key2 = '"234"'; 

this will lead to:

 { key: [ '"test"', '"234"' ] } 

I did not use CouchDB, maybe this will solve your problem.

0
source

Source: https://habr.com/ru/post/1240326/


All Articles