NodeJS : Need Help To build object with some Brackets and Braces - javascript

In my project I use request lib () and i would like to use qsStringifyOptions for query parametre but it's to much complicated in my case :/
In their documentation they link qs module to use qsStringifyOptions like them
I would like to have this URL
http://127.0.0.1:80/endpoint?foo=bar[baz:3]&aaa=[{baa:X,caa:XX,daa:[{eaa:Z,faa:ZZ}]}]&data[B]=BB&data[C]=CC
but I have this URL:
http://127.0.0.1:80/endpoint?foo=bar[baz:3]&aaa=[%7B%22baa%22:%22X%22,%22caa%22:%22XX%22,%22daa%22:[%7B%22eaa%22:%22Z%22,%22faa%22:%22ZZ%22%7D]%7D]&data[B]=BB&data[C]=CC
With this solution, the 'foo' attribute and data[B] and data[C] work fine but 'aaa' attribut not at all :
{ // some standar param URI, methode ...
useQuerystring: false,
qsStringifyOptions : {encodeValuesOnly: true, encode: false, indices: false},
qs : {
foo: 'bar[baz:1]',
aaa:'[{"baa":"X","caa":"XX","daa":[{"eaa":"Z","faa":"ZZ"}]}]',
data: {B:'BB', C:'CC'}
}
}
what i'm doing wrong? i just want it not to parse the 'aaa' attribute
thank you in advance for your help and your time

Related

How to use square brackets in nodejs (request-promise)?

it is my first post here, so please forgive me if I did something wrong.
So, I need to do a POST request in NodeJS. I choose request-promise for it. Everything was working like a charm until I needed to use square brackets in a key name. So I have the following code:
var options = {
method: 'POST',
uri: 'https://link.com',
form: {
ecomTeam[]: 'value',
ecomTeam%5B%5D: 'value', // tried this, doesn't work
'ecomTeam[]': 'value', // tried this, doesn't work
`ecomTeam[]`: 'value', // tried this, doesn't work
}
};
rp(options)
.then ...
How do I get it to work? I have tried a few solutions as seen in the snippet above - with no luck.
You can use brackets in an object key
var options = {
method: 'POST',
uri: 'https://link.com',
form: {
'ecomTeam[]': 'value',
'ecomTeam[]1': 'value1'
}
}
console.log(options.form['ecomTeam[]'])
console.log(options.form['ecomTeam[]1'])
like this

Duplicate keys in a REST connector query in loopback

I would like to ask if you know how could I duplicate parameters in a loopback REST connector query.
I have the following code:
details: {
'template': {
'method': 'GET',
'debug': true,
'url': 'https://www.example.com/data',
'timeout': 10000,
'headers': {
'Authorization': 'Bearer {token}'
},
'query': {
q: 'PHOTOS'
q: 'DETAILS',
id: '{id}'
},
'options': {
'useQuerystring': true
},
'responsePath': '$'
},
'functions': {
'searchData': [
'token',
'id'
]
}
}
The problem for that it is that it seems that loopback override the value of the parameter q by the last one, because I get only information for the last parameter.
Any idea how to solve it?
Thank you in avance.
You just have to pass them as an array:
'query': {
q: ['PHOTOS', 'DETAILS'],
id: '{id}'
},
Note that the options key, is passed to request and here's the documentation for useQuerystring:
useQuerystring - If true, use querystring to stringify and parse querystrings, otherwise use qs (default: false). Set this option
to true if you need arrays to be serialized as foo=bar&foo=baz
instead of the default foo[0]=bar&foo[1]=baz.
So if you remove it you'll end with something like ?q[0]=PHOTOS&q[1]=DETAILS.
You can also another option there:
qsStringifyOptions - object containing options to pass to the qs.stringify method.
Alternatively pass options to the
querystring.stringify
method using this format {sep:';', eq:':', options:{}}. For example,
to change the way arrays are converted to query strings using the qs
module pass the arrayFormat option with one of
indices|brackets|repeat
So you can actually end up with the same thing adding this:
"options": {
"qsStringifyOptions": {
"arrayFormat": "repeat"
}
}
And if you want to have just the brackets(something like this ?q[]=PHOTOS&q[]=DETAILS) you can specify brackets option:
"options": {
"qsStringifyOptions": {
"arrayFormat": "brackets"
}
}

ngResource setting const string after param

I'm trying to create a url that will query as:
http://localhost/api/:videoID/subtitles
However writing this in angularjs using ngResource transform it into a different "format":
$resource("api/videos/:id", {id: "#_id"}, {
getTracks: {method: "GET", url: "api/video/:video_id/tracks", params: {video_id: "#_id"}}
});
//results into: http://localhost/api/subtitles?videoID=:videoID
Does anyone know how to fix this exactly?
You need to change the route mask to following:
$resource("api/:id/subtitles", {id: "#id"});

Nested attributes with Angular.js

I have been racking my brain and google all morning trying to figure this out but I have come to the conclusion that I need to ask the experts! I am trying to do nested attributes with Sinatra and Angular, don't worry about the Sinatra side of things I am just trying to get the data to the server side in the correct manner at the moment. Please see the code below for an explanation of
My Input:
<input type="text" placeholder="{{item.placeholder}}" ng-model="question.possible_answer_attributes[$index][title]" class="form-control" />
My model object:
$scope.question = new Question({
poll_id: parseInt($routeParams.id),
title: '',
kind: 'open',
possible_answer_attributes: [] // I believe the issue may lie here
});
My factory:
.factory('Question', function($resource) {
return $resource('/api/questions/:id', { id: '#id' }, {
'update' : { method: 'PUT' },
'get_by_poll' : { method: 'GET', url: '/api/questions_by_poll/:id', isArray: true }
});
})
My object at time of running save function:
{"poll_id"=>1, "title"=>"123123", "kind"=>"multiple", "possible_answer_attributes"=>[{"undefined"=>"412312"}, {"undefined"=>"1234124"}, {"undefined"=>"234235234"}]}
I do not understand why my "possible_answer_attributes" keys are coming through as "undefined". It may be something very simple that I have missed, but any feedback would be great!
Thanks in advance!
In order to address the title property, you would need to use a string to index into the object:
ng-model="question.possible_answer_attributes[$index]['title']"
This should hold as long as possible_answer_attributes array looks like:
[{ title: 'my title' }]

Restangular: impossible to post complex json parameters?

I would like to send complex json to a put route:
Restangular.one('model3ds', model.uuid).put(
api_key: "blabla"
model3d: { is_public: true }
)
But it does not seems possible with Restangular, since it send:
api_key: "blabla"
model3d: "{\"is_public:\":true}"
Anyone knows how to change this behavior ?
Note: I can do that, but it's not possible if i have a full object to send, not only a property.
Restangular.one('model3ds', model.uuid).put(
"model3d[is_public]": true
)
Ok I found the (very complicated !) answer:
All the magic is in the custom keyword ;)
Restangular.one('model3ds', model.uuid).customPUT(
api_key: "blabla"
model3d: { is_public: true }
)
Another way without using customPUT is to provide the object as the second parameter of put method. For example:
var object = {
api_key: "blabla"
model3d: { is_public: true }
};
Restangular.one('model3ds', model.uuid).put(null, object);
More info: https://stackoverflow.com/a/17184167/1852787

Categories

Resources