Skip to content

Instantly share code, notes, and snippets.

@aaronksaunders
Last active November 9, 2018 04:51
Show Gist options
  • Save aaronksaunders/0215893de5ca978558e5 to your computer and use it in GitHub Desktop.
Save aaronksaunders/0215893de5ca978558e5 to your computer and use it in GitHub Desktop.
Accessing MongoDB Example Ionic Framework - based on hello sample
angular.module('starter.controllers', [])
.controller('DashCtrl', function ($scope) {
})
.controller('FriendsCtrl', function ($scope, Friends) {
Friends.init()
.then(function (_data) {
console.log(_data.data.deployment_id);
Friends.all().then(function (_data) {
$scope.friends = _data.data;
})
}, function (_error) {
console.log("error");
})
})
.controller('FriendDetailCtrl', function ($scope, $stateParams, Friends) {
Friends.init()
.then(function (_data) {
console.log(_data.data.deployment_id);
Friends.get($stateParams.friendId).then(function (_data) {
$scope.friend = _data.data;
})
}, function (_error) {
console.log("error");
})
})
.controller('AccountCtrl', function ($scope) {
});
[
{
"_id": {
"$oid": "4fcb0ccd1122e30001000005"
},
"name": "AndreaSun Jun 03 2012 03:05:45 GMT-0400 (EDT)",
"zip": "20011"
},
{
"_id": {
"$oid": "4fcb0d8e2bcdc90001000001"
},
"name": "AndreaSun Jun 03 2012 03:08:58 GMT-0400 (EDT)",
"zip": "20011"
},
{
"_id": {
"$oid": "4fcb0df31122e30001000006"
},
"name": "AndreaSun Jun 03 2012 03:10:39 GMT-0400 (EDT)",
"zip": "20011"
},
{
"_id": {
"$oid": "4fcb0e0a2bcdc90001000002"
},
"name": "AndreaSun Jun 03 2012 03:11:03 GMT-0400 (EDT)",
"zip": "20011"
},
{
"_id": {
"$oid": "4fcacbac33256c0008000005"
},
"name": "Updated NameSun Jun 03 2012 11:42:28 GMT-0400 (EDT)",
"zip": "20011"
},
{
"_id": {
"$oid": "4fd4e4472dfe6a0001000002"
},
"name": "Andrea Sun Jun 10 2012 14:15:05 GMT-0400 (EDT)",
"zip": "20011",
"age": 100
},
{
"_id": {
"$oid": "4fd4e6dc57ed4b0001000001"
},
"name": "Andrea Sun Jun 10 2012 14:26:35 GMT-0400 (EDT)",
"zip": "20011"
},
{
"_id": {
"$oid": "50f328fe0f2a930002000032"
},
"name": "Bryce Saunders",
"age": 9
},
{
"_id": {
"$oid": "50f36f25866d3f000200001d"
},
"name": "Reina Saunders",
"age": 2
}
]
angular.module('starter.services', [])
/**
* A simple example service that returns some data.
*/
.factory('Friends', function ($http) {
// Might use a resource here that returns a JSON array
var APIKEY = "wdoRx9E5TffPOmIdFA9RhTz4I91mx9vnk4pJuDeybtU";
var HEADER_VALUES = {"Authorization": "api-key " + APIKEY, "Accept": "application/json"};
var deploymentId;
var getCallback = function (data, status, headers, config) {
console.log(JSON.stringify(data, null, 2));
return data;
};
return {
init: function () {
// get the deployment id
var url = "https://beta-api.mongohq.com/databases/test_aks";
return $http.get(url, { headers: HEADER_VALUES})
.success(function (_data) {
console.log(_data.deployment_id)
deploymentId = _data.deployment_id;
return deploymentId;
});
},
all: function () {
var url = "https://beta-api.mongohq.com/mongo/" + deploymentId + "/test_aks/collections/my_users/documents";
return $http.get(url, { headers: HEADER_VALUES}).success(getCallback);
},
get: function (friendId) {
var url = BASE_URL + "/collections/my_users/documents/" + friendId;
return $http.get(url, { headers: HEADER_VALUES}).success(getCallback);
}
};
});
<ion-view title="Friends">
<ion-content class="has-header">
<ion-list>
<ion-item ng-repeat="friend in friends" type="item-text-wrap" href="#/tab/friend/{{friend.id}}">
{{friend.name}}
</ion-item>
</ion-list>
</ion-content>
</ion-view>
@kosomi
Copy link

kosomi commented Dec 11, 2015

where do I have to put data.json file?

@GaneshPediredla
Copy link

How can i get the data from mongodb using nodejs in ionic2?

@Babuvijay123
Copy link

fbfgfgh

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment