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

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

Related

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

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

Unit test for creation object

I'm started to learn unit test using jasmine. And maybe someone can explain me - how I need to check creating new object like:
const outLog = {
time: newTime,
request: {
direction: 'OUT',
method: data.request.method,
title: data.request.url,
body: data.request.data,
headers: getHeaders(data.request.headers),
},
response: {
status: '',
headers: {},
body: {},
},
};
if (data.response && data.response !== null) {
outLog.response.status = data.response.status;
outLog.response.body = data.response.data;
outLog.response.headers = getHeaders(data.response.headers);
}
log.out.push(outLog);
Do I need to fill the fields like request method, request title with real data and check
const outLog = {
request: {
method: 'POST',
title: 'title',
...
...
},
}
expect(outLog.request.method).toEqual('POST');
expect(outLog.request.title).toEqual('title');
Thanks!
For checking objects, most frameworks usually provide some sort of pretty diff when you compare two objects.
To get this you'd first need to compare it to an object literal:
expect(outLog).toEqual({
request: {
method: 'POST',
title: 'title',
...
})
This also removes a LOT of logic from all the expect statements, which should help with maintenance and debugging. Additionally, when there are many different assertions, the earlier ones mask the later ones, creating unnecessary feedback loops, when the same thing could be accomplished with a single assertion. one of the unit testing thought leaders addresses this by recommending that unit tests should only ever have a single assertion to help keep them focused, reduce debugging time, and make them easier to work on and understand.

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' }]

get the expanded row id in postData jqgrid

I'm strugling to get the expanded row id in a jqgrid postData, I want something like:
postData: {
objectID: expandedRowId
}
how can i get the expandedRowId to send it to server with my parameters. I've seen something like:
postData : {
...
nodeid:rc.id,
n_left:rc.lft,
n_right:rc.rgt,
n_level:rc.level,
...
}
but what is "rc"??
plzz help
Sorry, but it's unclear what you want. I have to guess what you mean. Is expandedRowId a variable which you defined in your code and which you change somewhere? In the case you can define objectID property of postData as function:
postData: {
objectID: function () { return expandedRowId; }
}
see the answer for more details.

Categories

Resources