I'm using the MEAN stack to create a web app. I have my server.js, console.js, a MongoDB called 'contactlist', and an index.html that contains Javascript for a tree diagram. What I would like to do is query my MongoDB for say, a key, and return with its value (or really just grab any information). I'd like to then save that value as a variable and pass it along to the javascript contained in my index.html.
This is the closest I've gotten reconstructing a tutorial's code. Using mongojs as my driver, I have this in my server.js:
app.get('/contactlist', function (req, res) {
console.log("I received a GET request")
db.contactlist.find({email:"[email protected]"},function(err, docs) {
//looks at the contact list database for anything with the email of [email protected]
console.log(docs);
res.json(docs);
});
});
In my controller:
var myApp = angular.module('myApp', []);
myApp.controller('AppCtrl', ['$scope', '$http', function($scope, $http) {
console.log("Hello World from controller");
var refresh = function() {
$http.get('/contactlist').success(function(response) {
console.log("I got the data I requested");
$scope.contactlist = response;
$scope.contact = "";
});
};
The use of the ng-repeat function below is an artifact of the original application's use (display a list of all contacts). Now I just want one value, but I don't know what function to use. Here's my index.html:
<div class="container" ng-controller="AppCtrl">
<table class="table">
<thead>
<tr>
</thead>
<tbody>
<tr ng-repeat="contact in contactlist">
<td>{{contact.name}}</td>
<!-- grabs the name from parameters set out in server.js -->
</tr>
</tbody>
</table>
</div>
This displays the name ("Bob") of the email I filtered for ([email protected]) onto the webpage. At this point, even just being able to save that name as a variable and pass it along to the javascript would help. Ng-repeat is definitely not what I want to use, but I'm new to programming and the MEAN stack so I'm kind of stuck.
Thanks.