How to get Values of Response ReactJs - json

Hey guys im pretty new in developing with react.
I got some problems to fetch data from my json response
axios.get('http://localhost:9000/test')
.then(function (response) {
console.log(response.data.name);
console.log(response.name)
});
{
"name": "mkyong",
"age": 30,
"address": {
"streetAddress": "88 8nd Street",
"city": "New York"
},
"phoneNumber": [
{
"type": "home",
"number": "111 111-1111"
},
{
"type": "fax",
"number": "222 222-2222"
}
]
}
The console shows me undefined.
Whats the problem why i cant get the data via plain js?

well because the response of an axios call is a string, so you have to parse it first,
axios.get('http://localhost:9000/test')
.then(function (response) {
const parsedResponse = JSON.parse(response);
console.log(parsedResponse.name));
});

In my case, something like this worked:
axios.get('http://localhost:9000/test')
.then(function (response) {
console.log(response.data['name']);
});
There may be different levels of the json tree, so in my case I first tried displaying just response.data to see what's inside & got to the element this way.

Are you sure its not
axios.get('http://localhost:9000/test')
.then(function (response) {
console.log(response['data'][name]);
console.log(response[name])
});

Related

get json value object from mongodb

I have formData node that has dynamic jsonObject value in mongodb
{
"_id": {
"$oid": "5a71fea0f36d2848ae4f8f0a"
},
"formData": {
"pages": [
{
"name": "page1",
"questions": [
{
"type": "comment",
"name": "about",
"title": "Please tell us about your main requirements "
}
]
}
]
},
"editorId": "59678e58f36d2842f777bc48",
"datetimeSubmit": "2018/01/15"
}
I write a node API to fetch the data from mongodb, it only display ids, editorI and datetimesubmit nodes, but it ignores the formData(jsondata) field.
const Model = require('./myModel');
module.exports = {
getData: function (callback) {
Model.find({}, (err, jsonObj) => {
callback(null, {
data: jsonObj
})
})
}
}
looks like the model.find() doesn't return jsonObject value?
thanks
got my own question fixed, basically, i should also define the data type as JSON in schema, otherwise, will be ignored.

how to get length of couch.get(data,headers,status)

I tried to create nodejs with expressjs by connecting to couchdb . For that I have used nodejs-couch installed using npm.
app.get('/', function(req, res) {
couch
.get(dbName, viewUrl)
.then(
function(data, headers, status) {
console.log(data.length);
var data1 = data.data.rows;
console.log(data1);
res.send(data1.length);
},
function(err) {
res.send(err);
}
);
});
my json data :
[
{
"id": "7a6a4d9cf76efc00977ca63ca3002a31",
"key": "7a6a4d9cf76efc00977ca63ca3002a31",
"value": {
"name": "nagarjuna",
"email": "nagarjuna#gmail.com",
"password": "12345",
"mobile": "987654321",
"rev": "8-c90103f1fca709ae314a684c767c97dc"
}
},
{
"id": "7a6a4d9cf76efc00977ca63ca3003743",
"key": "7a6a4d9cf76efc00977ca63ca3003743",
"value": {
"name": "sandeep",
"email": "sandeep#gmail.com",
"password": "54321",
"mobile": "9876541",
"rev": "1-fd6e667b87794adea78e169bc46016e6"
}
}
]
I had written get function to fetch all customer information. If I print console.log(data.data.rows); I am able fetch json data. But I want to get length and attribute values from json. But when I print console.log(data.length); it getting undefined.
Can any one suggest me. Any suggestions would be appreciated.
The result from the database contains a lot of different information, like meta information and the results. Here is a trimmed down representation of what you're looking for
var data = {
data: {
rows: [{row1},{row2}...];
}
}
So for you to be able to get the length of rows from the database, you'll have to dig into the right attribute in the object like this:
var length = data.data.rows.length;

Im trying to get values of the following JSON object and display them in html using angularness

In the following JSON object, I'm trying to get the value of the streets
Get business profile data :
{
"_id": "56dd1bd4d0561b403f683bfd",
"UserId": "56b301516b9064189049acb5",
"WebURL": "2",
"PhoneNumber": "3",
"ContactEmail": "1#gmail.com",
"AdditionalInfo": "",
"__v": 0,
"DateCreated": "2016-03-07T06:12:36.957Z",
"Categories": [],
"Addresses": [{
"Street": "1",
"City": "1",
"State": "1",
"Country": "1",
"SuiteNumber": "1",
"_id": "56dd1bd4d0561b403f683bfe"
}]
}
So far this is what I have:
Info.GetData()
.success(function (response){
$scope.Data = response;
})
.error(function (response, status) {
alert("Can Not Retrieve Company Info, Please Contact Admin")
})
<div ng-repeat="x in Data.Addresses">
{{x.Street}}
</div>
I still can't get it working.
Assuming your Info.GetData() is making a $http call and you are using success instead of then, you will still have to use then wherever there is a promise. If you are doing this
return
angular.module('app').factory('Info', function($http) {
return {
getData: function() {
return $http.get('url');
};
};
});
in your Info.getData(). You could instead do
return
angular.module('app').factory('Info', function($http) {
return {
getData: function() {
return $http.get('url').then(function(response)
{
return response.data;
}
};
};
});`
Also, you need to check if your service is returning a promise. If its a promise you should resolve it. You can't assume that your promise has a .success().

AngularJS getting in trouble with my JSON

I have got a JSON object from my website:
{ "ID":"102”,
"role":{“subscriber”:true},
"first_name”:”Test 3”,
"last_name”:”Test 4”,
"custom_fields":{ “job_title”:”testing”},
}
and AngularJS to manage the dynamic content but it doesn't seem to be working:
var app = angular.module('myApp', []);
function PeopleCtrl($scope, $http) {
$scope.people = [];
$scope.loadPeople = function () {
var httpRequest = $http({
method: 'POST',
url: '/echo/json/',
data: mockDataForThisTest
}).success(function (data, status) {
$scope.people = data;
});
};
}
Here is a JSFiddle.
Can anybody help me with displaying data?
#qqruza to get the callback working properly in your jsfiddle.net/1zuteco7, change the url to this:
http://test.eventident.com/api/last_posts/siteid=&callpage=1&perpage=10&callback=JSON_CALLBACK
Notice the JSON_CALLBACK in the end. The rest of your app will still not work though cause you are not picking the right bindings from the returned data in your repeat directive. Try console.log(data) in the success function to click through the returned object and get to the right paths.
There were a number of issues with your JSON, I have resolved them.
It had different types of quotes in there. I have replaced them with ".
It now looks like this:
[{         
"ID": "100",
"role": {            
"subscriber": true         
},
"first_name": "Test",
"last_name": "Test2",
"custom_fields": {            
"job_title": "subscriber"         
},
}, {   
"ID": "102",
"role": {            
"subscriber": true         
},
"first_name": "Test 3",
"last_name": "Test 4",
"custom_fields": {            
"job_title": "testing"         
},       
}]
Also, you were not referencing the model fields correctly in your view.
Here is the updated working fiddle: http://jsfiddle.net/kmmmv83y/1/
You had a comma at the end of the last property, that will typically error everything out, the below JSON should work:
{ "ID":"102”,
"role":{“subscriber”:true},
"first_name”:”Test 3”,
"last_name”:”Test 4”,
"custom_fields":{ “job_title”:”testing”}
}

AngularJS display JSON data in table

I am using AngularJS with RESTAPI, I am able to get the customers data sent from server in a JSON format. My Angular code snippet as below:
app.factory("services", ['$http', function($http) {
var serviceBase = '/api/album';
var obj = {};
obj.getCustomers = function(){
return $http.get(serviceBase);
};
return obj;
}]);
app.controller('listCtrl', function ($scope, services) {
services.getCustomers().then(function(data){
alert(JSON.stringify(data.data));
$scope.customers = data.data;
});
});
Here is my JSON data:
{
"data": [
{
"id": "1",
"artist": "Gotye75",
"title": "Making Mirrors7"
},
{
"id": "100",
"artist": "ytttt5",
"title": "1231"
},
{
"id": "101",
"artist": "65",
"title": "565444555"
},
{
"id": "102",
"artist": "6",
"title": "6"
},
{
"id": "103",
"artist": "y",
"title": "yy"
},
{
"id": "104",
"artist": "ty",
"title": "yt"
},
{
"id": "109",
"artist": "ytrer",
"title": "yt"
}
]
}
I am able to display the JSON data in table if my JSON does not contain the "data" hear. However if my jSON data comes with "data" header, it is unable to display. I am asking, what are the solution in Angular to parse the JSON object.
For example if it is in BackboneJS, i can simply do
parse: function (response) {
//alert(JSON.stringify(response.data));
return response.data;
}
How can i do it in Angular?
$http resolves its promises with a response object. The data is accessed via the response object's data property. So to get to the array, you'd have to use
$scope.customers = data.data.data;
$http's promise is enhanced with a .success() convenience method which unwraps the response object...
services.getCustomers().success(function(data){
alert(JSON.stringify(data.data));
$scope.customers = data.data;
});
I solved it by
app.controller('listCtrl', function ($scope, services) {
services.getCustomers().then(function(data){
//notice that i added the third data
$scope.customers = data.data.data;
});
});