How to store multiple json objects in angular7 - angular7

How to store multiple json objects in to an array in angular7?
I am trying to save array of json objects like below :
employees: any;
ngOnInit() {
this.getemployee().subscribe
data => this.employees == data,
);
}
below is the response i am getting from the backend :
(32) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]
0: {id: "59744", employee_name: "Sample API ", employee_salary: "18000", employee_age: "20", profile_image: ""}
1: {id: "59747", employee_name: "Test", employee_salary: "123", employee_age: "456", profile_image: ""}
2: {id: "59748", employee_name: "hello#amdon", employee_salary: "10000", employee_age: "35", profile_image: ""}
.......

create an array of objects of the following structure.
//response data structure
export class ResponseData {
id:string
employee_name: string;
employee_salary: string;
employee_age: string;
profile_image:string;
}
component.ts
export class Test implements OnInit {
responseData:ResponseData[]=[];
constructor(){ }
ngOnInit() {
this.getemployee().subscribe((data)=>{
if(data != null) {
console.log('response received');
this.responseData = data;
}
});
}
}

Try this solution:
Component.ts:
employees: IEmployeeData[] = [];
constructor(){ }
ngOnInit() {
this.getemployee().subscribe((data: IEmployeeData) => {
if(data) this.employees = data;
});
}
interface IEmployeeData {
id:string
employee_name: string;
employee_salary: string;
employee_age: string;
profile_image:string;
};

Related

Error when I'm trying to do a POST request in Fiware

I'm developing a script to do a POST request to Orion Context Broker of Fiware. The problem is that a receive an error:
"ContextLengthRequired"
, but I have Context-Length header in the request.
This is the error when I tried node fiware.js
{
"error":"ContentLengthRequired",
"description":"Zero/No Content-Length in PUT/POST/PATCH request"
}
This is the code in the file fiware.js:
var request = require('request');
var entity = {
"id": "Room6",
"type": "Room",
"temperature": {
"value": 23,
"type": "Float"
},
"pressure": {
"value": 700,
"type": "Float"
}
};
var jsonObject = JSON.stringify(entity);
var aux = jsonObject.toString();
aux = aux.length;
var peticion = {
url: "http://127.0.0.1:1026/v2/entities",
method: "POST",
headers: {
"Content-Length": aux,
"Content-Type": "application/json"
},
data: jsonObject
};
request(peticion, function(error, response, body) {
console.log(error);
console.log(body);
});
Change the variable peticion from this:
var peticion = {
url: "http://127.0.0.1:1026/v2/entities",
method: "POST",
headers: {
"Content-Length": aux,
"Content-Type": "application/json"
},
data: jsonObject
};
To this one:
var peticion = {
url: "http://127.0.0.1:1026/v2/entities",
method: "POST",
headers: {
"Content-Length": aux,
"Content-Type": "application/json"
},
body: jsonObject
};

How to normalize this recursive nested JSON

Having a bit of an issue trying to normalise the my payload that contains a nested schema of the same type as the parent using Normalizr
For example
{
id: 123,
sections:{
section: [{
id: 1,
name: "test",
sections: {
section: {
id: 125,
name: "test125"
}
}
}, {
id: 2,
name: "test2"
sections: {
section: [
{
id: 124,
name: "test124"
}
]
}
}, {
id: 3,
name: "test3"
}]
}
}
In the above json structure, nested section may be an object or an array.
I had a similar issue with nested comments. Here's how I solved it:
export const comment = new schema.Entity("comment", {}, {
idAttribute: "key"
})
comment.define({
user: user,
reactions: {
data: [reaction]
},
children: [comment]
})
export const post = new schema.Entity("post", {
user: user,
files: [image],
comments: {
data: [comment]
},
reactions: {
data: [reaction]
}
}, {
idAttribute: "key"
})
Basically, I define comment as a new entity before I use it in its own entity definition. The other schemas I define as usual, in the constructor (see the post schema for an example). Hope this helps.
Here is a jq filter which will normalize the data:
def enumerate:
if type=="array" then .[] else . end ;
def sectionids:
[ .sections.section | enumerate | .id // empty | tostring ]
| if .==[] then {} else {sections:.} end ;
def sections:
{sections:{section:[.]}}
| .. | .section? | enumerate | objects | del(.sections) + sectionids ;
{
"result": .id,
"entities": {
"sections": (reduce sections as $s ({};.["\($s.id)"]=$s))
}
}
Sample Run (assumes proper json data in data.json and the above filter in filter.jq)
$ jq -M -f filter.jq data.json
{
"result": 123,
"entities": {
"sections": {
"123": {
"id": 123,
"sections": [
"1",
"2",
"3"
]
},
"1": {
"id": 1,
"name": "test",
"sections": [
"125"
]
},
"2": {
"id": 2,
"name": "test2",
"sections": [
"124"
]
},
"3": {
"id": 3,
"name": "test3"
},
"125": {
"id": 125,
"name": "test125"
},
"124": {
"id": 124,
"name": "test124"
}
}
}
}
Try it online!

React-native axios method POST response.data null

I use React-native for my school project and axios to take data.
here is my function :
login(username, password){
axios({
method : 'post',
url : 'http://xxxx.xxxx.com/UserRestController.php',
data : {
accountId : 2
}
})
.then(function (response) {
console.log('data', response.data);
})
.catch(function (error) {
console.log(error);
});
}
when I try to show response, this is always null.
Like this
data Object {
"config": Object {
"adapter": [Function xhrAdapter],
"data": "{\"accountId\":2}",
"headers": Object {
"Accept": "application/json, text/plain, */*",
"Content-Type": "application/json;charset=utf-8",
},
"maxContentLength": -1,
"method": "post",
"timeout": 0,
"transformRequest": Object {
"0": [Function transformRequest],
},
"transformResponse": Object {
"0": [Function transformResponse],
},
"url": "http://ariary.vola.mg/UserRestController.php",
"validateStatus": [Function validateStatus],
"xsrfCookieName": "XSRF-TOKEN",
"xsrfHeaderName": "X-XSRF-TOKEN",
},
"data": "",
"headers": Object {
"access-control-allow-origin": "*",
"connection": "keep-alive",
"content-type": "text/html; charset=UTF-8",
"date": "Mon, 31 Jul 2017 13:29:28 GMT",
"server": "nginx",
"transfer-encoding": "chunked",
"vary": "Accept-Encoding",
"x-powered-by": "EasyEngine 3.7.4",
},
"request": XMLHttpRequest {
"DONE": 4,
"HEADERS_RECEIVED": 2,
I use axios.GET in another function, and it works great. Can someone help me?
Thanks
Try parsing the response to json before accessing it's data like this:
axios({
method : 'post',
url : 'http://xxxx.xxxx.com/UserRestController.php',
data : {
accountId : 2
}
})
.then((res) => res.json())
.then(function (response) {
console.log('data', response.data);
})
.catch(function (error) {
console.log(error);
});
After some test, my problem is not the response but my data. This is always null. the accountId is undefined in my server. But I don't know why?

Cypher results return certain structure

I'm trying to return a certain structure.
Here is my query:
MATCH (tracker:tracker { active: true }) OPTIONAL MATCH (tracker { active: true })--(timer:timer) RETURN { tracker:tracker, timers:COLLECT(timer) } as trackers
Here is what I am returning so far:
{
"results": [{
"columns": ["trackers"],
"data": [{
"row": [{
"tracker": {
"title": "a",
"id": "04e3fddc-5aef-4c3a-9aeb-62a9fb15bd75",
"active": true
},
"timers": []
}]
}]
}],
"errors": []
}
I would like the timers to be nested under the "tracker" with the tracker's properties, like this:
{
"results": [{
"columns": ["trackers"],
"data": [{
"row": [{
"tracker": {
"title": "a",
"id": "04e3fddc-5aef-4c3a-9aeb-62a9fb15bd75",
"active": true,
"timers": []
}]
}]
}],
"errors": []
}
Try this:
MATCH (tr:tracker {active: true})
OPTIONAL MATCH (tr)--(ti:timer)
WITH {
title: tr.title,
id: tr.id,
active: tr.active,
timers: COLLECT(ti)
} as trackers
RETURN trackers

openlayers 3 try to load geojson object

Im a bit new to workng with openlayers 3.
im trying to load a geojson object in openlayers 3 but it display the center only point[0,0], which not even in my geojson object, where im i going wrong.
the code is below:
//CREATE A BASE LAYER
var raster = new ol.layer.Tile({
source: new ol.source.OSM()
});
// create a source from the feature
var source = new ol.source.GeoJSON(
({
object:
{
'type': 'FeatureCollection',
'crs': {
'type': 'name',
'properties': {'name': 'EPSG:3857'}
},
'features': [
{
'type': 'Feature',
//'properties': {'id': 63},
'geometry': {
'type': 'Point',
'coordinates': [12, 12]
}
},
{
'type': 'Feature',
'properties': {'id': 62},
'geometry': {
'type': 'Point',
'coordinates': [35.0, 1.0]}
},
{
'type': 'Feature',
'properties': {'id': 61},
'geometry': {
'type': 'Point',
'coordinates': [34.0, 0.0]}
},
{
'type': 'Feature',
'properties': {
'id': 56
},
'geometry': {
'type': 'Point',
'coordinates': [33.0, 33.0]
}
}
]
}
})
);
// CREATE THE LAYER WITH THE DATA
var vectorLayer = new ol.layer.Vector({
source: source
});
// create an openlayers map object
var map = new ol.Map({
target: attrs.id,
layers: [raster,vectorLayer],
view: new ol.View({
//projection:'EPSG:4326',
center:[0,0],
zoom:2
})
});
}
i figured it out, i had to do a reprojection of my geojson object,
openlayers3 by default uses epsg:3857,so does my base map which is openstreetmap i.e projected coordinate system, so i had to turn my geojson to a projected system since it was coming in using the longitude and latitudes i.e. geographical coordinate system.(geojson is always espg:4326)
var source = new ol.source.GeoJSON({
object:{
'type': 'FeatureCollection',
'crs': {
'type': 'name',
'properties': {'name': 'EPSG:4326'}
},
'features': [
{
'type': 'Feature',
//'properties': {'id': 63},
'geometry': {
'type': 'Point',
'coordinates': [12, 12]
}
},
{
'type': 'Feature',
'properties': {'id': 62},
'geometry': {
'type': 'Point',
'coordinates': [35.0, 1.0]}
},
{
'type': 'Feature',
'properties': {'id': 61},
'geometry': {
'type': 'Point',
'coordinates': [34.0, 0.0]}
},
{
'type': 'Feature',
'properties': {
'id': 56
},
'geometry': {
'type': 'Point',
'coordinates': [33.0, 33.0]
}
}
]
},
projection: 'EPSG:3857'
}
);

Resources