express get all values as strings from angular POST request - angularjs

I have a following simple code that send a POST request to a server with express:
$http.post('/blah', {
boolean: true,
stringBoolean: 'true',
number: 213,
stringNubmer: '44444444',
string: 'adssd',
arrayNumber: [1, 2, 3, 4],
arrayBoolean: [true, false, "true", "false"],
});
and this lines on server side:
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({extended: true}))
app.post('/blah', (req, res)=>{
console.log(req.body)
})
the output in console after client send request(tested on chrome and firefox) will be with all values as string:
{ boolean: 'true',
stringBoolean: 'true',
number: '213',
stringNubmer: '44444444',
string: 'adssd',
arrayNumber: [ '1', '2', '3', '4' ],
arrayBoolean: [ 'true', 'false', 'true', 'false' ] }
Then I use Postman to send same data structure to server:
{
"boolean": true,
"stringBoolean": "true",
"number": 213,
"stringNubmer": "44444444",
"string": "adssd",
"arrayNumber": [1, 2, 3, 4],
"arrayBoolean": [true, false, "true", "false"]
}
but this time I have numbers and booleans in console:
{ boolean: true,
stringBoolean: 'true',
number: 213,
stringNubmer: '44444444',
string: 'adssd',
arrayNumber: [ 1, 2, 3, 4 ],
arrayBoolean: [ true, false, 'true', 'false' ] }
Seems like angular doing some conventions behind the curtains. How can I avoid that and get correct values types send to server?
UPDATE:
In chome console I can see request body in raw format wich look like this:
boolean=true&stringBoolean=true&number=213&stringNubmer=44444444&string=adssd&arrayNumber%5B0%5D=1&arrayNumber%5B1%5D=2&arrayNumber%5B2%5D=3&arrayNumber%5B3%5D=4&arrayBoolean%5B0%5D=true&arrayBoolean%5B1%5D=false&arrayBoolean%5B2%5D=true&arrayBoolean%5B3%5D=false
which mean each value will always be string and I have to manually convert it on server side to appropriate types. Is this questing right? What are the good practices here?

it is using JSON.stringify() internally. use JSON.parse()

Related

How to display key value pairs from nested object

I have this structure of data (its more nested, but I give you example of it)
{
status: {
observedGeneration: 2,
replicas: 1,
updatedReplicas: 1,
readyReplicas: 1,
availableReplicas: 1,
conditions: [
{
type: "Progressing",
status: "True",
lastUpdateTime: "2022-02-25T09:28:17Z",
lastTransitionTime: "2022-02-25T09:19:35Z",
},
{
type: "Available",
status: "True",
lastUpdateTime: "2022-09-06T02:03:09Z",
lastTransitionTime: "2022-09-06T02:03:09Z",
}
]
}
settings: {
observedGeneration: 2,
replicas: 1,
updatedReplicas: 1,
readyReplicas: 1,
availableReplicas: 1,
conditions: [
{
type: "Progressing",
status: "True",
reason: "NewReplicaSetAvailable",
message: "ReplicaSet \"label-studio-546655c5cc\" has successfully progressed.
},
{
type: "Available",
status: "True",
reason: "MinimumReplicasAvailable",
message: "Deployment has minimum availability."
}
]
}
}
How I can iterate over this to display in React structure like key value pairs. For example:
This output is only example, but what I want to display is
`key`: `value`
`key`: `value'
`key`:
`key`:`value`
name: Mathew
seetting:
label: 1
composie: yes
additionalSetting:
performance: no
ready: onlyData
version: 123
mountPath: replices
I just was thinking about recursion function, but after many hours of struggling I think I need a little bit of help :)
Thank you so much for any solutions :)
As suggested in comment you can use a tree structure with primitive values as leaf. See this codesandbox for an example of such structure.

How can I display json data in reactjs

My JSON data looks like this:
safarisDetails: {
loading: false,
safaridetails: {
safari: [
{
id: 1,
safari_name: '3 DAYS GORILLA TREKKING TRIP',
days: 3,
days_category: '<4',
budget: '900',
title: '3 DAYS GORILLA TREKKING TOUR',
text: 'Test.',
travelertype: '',
date_created: '2021-10-08T15:22:31.733347Z'
}
],
highlight: [
[
{
id: 1,
highlight: 'See the famous mountain gorillas',
safaris: 1
},
{
id: 2,
highlight: 'Get to know the Batwa community',
safaris: 1
}
]
]
I want to render objects in my component such as the safari_name and loop trough the highlights. However, when I use {safarisdetails.safari.safari_name} i get an error message stating: 'safari_name' is not defined.
There are a couple of problems based on the same you provided...
safarisdetails is repeated twice in the JSON, but not in your check
safari is an array in the JSON, but accessed like an object in your check
For example, to get the first name, you'd write something like:
safarisdetails.safarisdetails.safari[0].safari_name
Also, is this JSON even valid? Maybe you posted a partial snippet, but the JSON has to have {} around the whole thing.

How do i modify a raw data object returned by an ExtJS AJAX proxy into a JSON object to be consumed by a Tree Store

In an effort to create a treepanel, i configure it with a treestore whose AJAX proxy url receives json data i have no control of. But using Ext.data.reader.Json's transform property invokable before readRecords executes, gives an option to modify the passed raw (deserialized) data object from the AJAX proxy into a modified or a completely new data object. The transform config, gives the code snippet below:
Ext.create('Ext.data.Store', {
model: 'User',
proxy: {
type: 'ajax',
url : 'users.json',
reader: {
type: 'json',
transform: {
fn: function(data) {
// do some manipulation of the raw data object
return data;
},
scope: this
}
}
},
});
I would please like an example on how to go about modifying the return JSON object
[
{
"id": 3,
"attributes":
{},
"name": "user_one",
"login": "",
"email": "user_one#ats",
"phone": "0751223344",
"readonly": false,
"administrator": false,
"password": null
},
{
"id": 4,
"attributes":
{},
"name": "user_two",
"login": "",
"email": "user_two#ats",
"phone": "0751556677",
"readonly": false,
"administrator": false,
"password": null
}
]
into a JSON object fit for a treestore.
The hierarchical tree is to be rendered to show which user is under which admin using a condition administrator==true from the returned JSON, then a second AJAX request that returns that admin's users shown here.
[
{
"user_id": 3,
"admin_id": 1,
},
{
"user_id": 4,
"admin_id": 2,
}
]
Is the data nested at all? Otherwise why use a treepanel instead of a grid? To your question though, it'll depend on how you configure your treepanel but it would probably be something like this:
transform: {
fn: function(data) {
var treeRecords = Ext.Array.map(data, function(i){
return {
text: i.name,
leaf: true
//any other properties you want
}
});
var treeData = {
root: {
expanded: true,
children: treeRecords
}
};
return treeData;
},
scope: this
}

Set criteria in query for fields and fields in nested objects

I have a document like this:
{
"InDate": "11.09.2015",
"Kst2Kst": true,
"OutDate": "11.09.2015",
"__v": 0,
"_id": ObjectId('55f2df2d7e12a9f1f52837e6'),
"accepted": true,
"inventar": [
{
"accepted": "1",
"name": "AAAA",
"isstammkost": true,
"stammkost": "IWXI"
},
{
"accepted": "1",
"name": "BBBB",
"isstammkost": false,
"stammkost": "null"
}
]
}
I want to select the data with "isstammkost": true in the inventar-array.
My query is:
Move.findOne({accepted : true, 'inventar.isstammkost' : true},
'OutDate InDate inventar.name', function(err, res)
It doesn't work -> It selects all, even with inventar.isstammkost : false.
The "normal" query works like I want (without criteria in sub-array). Whats the right way to set criteria in sub-array?
Of course it will return the "isstammkost": false part, because that is part of the same document as the "isstammkost": true. They are both objects in the array "inventar", a top-level field in a single document. Without some sort of projection, the entire document will always be returned to a mongodb query and thus nodejs will pass them on to you.
I'm not terribly up-to-speed on nodejs, but if this were the mongo shell it would look like this:
> db.MyDB.findOne({{accepted : true, "inventar.isstammkost" : true}, {"inventar.isstammkost.$": 1});
You will need to find out how to add that extra parameter to the nodejs function.

Django database introspection

I'd like to introspect the Django database(or table) at the runtime. So, for example- I'd like to do something like:
>>> a = django.db.introspect()
and now *a* should see like
a = {
'table_name1':{
'column_name_1_1':{
'index': True,
'unique': True,
'pk': True
},
'column_name_1_2':{
'index': True,
'unique': False,
'pk': False
}
},
'table_name2':{
'column_name_2_1':{
'index': True,
'unique': True,
'pk': True
},
'column_name_2_2':{
'index': True,
'unique': False,
'pk': False
}
}
}
And- I'd like to do that with Django & South and without any 3rd party tools(I know that I coul do that with SQLAlchemy). I want to introspect the actual db, not the frozen one in my last migration. Is that possible? How can I start?
I've found my answer- here is everything I need:
https://code.djangoproject.com/browser/django/trunk/django/core/management/commands/inspectdb.py

Resources