angularjs - how to select values from mongodb using mongoose -
i using meanstack try fetch data mongodb of angularjs new in technology.i insert values correctly want retrieve mongodb-mongoose
i using meanstack try fetch data mongodb of angularjs new in technology.i insert values correctly want retrieve mongodb-mongoose
var app = angular.module("app", [ ]); app.controller('mongoosecontroller', ['$scope', '$http' ,'$window', '$filter', function ($scope, $http, $window, $filter) { debugger; var refresh = function () { $http.get('/viewuser').then(function (response) { $scope.viewusers = response.data; }); }; refresh(); $scope.addnewdetails = function ( ) { $http.post('/addnewdetails', $scope.user).then(function (response) { }); }; }]); server var express = require('express'); var app = express(); var mongoose = require('mongoose'); var schema = mongoose.schema(); var bodyparser = require('body-parser'); app.use(express.static(__dirname + "/public")); app.use(bodyparser.json()); mongoose.connect('mongodb://localhost/crud'); var schema = new mongoose.schema({ email: string, name: string, age: number, date: { type: date, default: date.now } }); var user = mongoose.model('emps', schema); app.get('/viewuser', function (req, res) { user.find({}, function (err, docs) { console.log(docs); }); }); app.post('/addnewdetails', function (req, res) { new user({ email: req.body.email, name: req.body.name, age: req.body.age }).save(function (err, doc) { if (err) { res.json(err); } else { res.send('successfully inserted!'); } }); }); app.listen(8082); console.log("server running on port 8082");
<!doctype html> <html ng-app="app"> <head> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.3/angular.min.js"></script> <script src="controller/mongoosecrud.js"></script> </head> <body > <div ng-controller="mongoosecontroller"> <label for="email">email: </label> <input type="email" name="email" ng-model="user.email" /><br /> <label for="name">name: </label> <input type="text" name="name" ng-model="user.name" /><br /> <label for="age">age: </label> <input type="number" name="age" ng-model="user.age"/><br /> <input type="button" value="submit" ng-click="addnewdetails()"> <ul ng-repeat="viewuser in viewusers"> <li>{{viewuser.name}}</li> </ul> </div> </body> </html>
it should be,
user.find({}, function(err, docs) { console.log(docs); }
also change request as,
$http.get('/viewuser').then(function (response) { $scope.viewusers = response.data; });
Comments
Post a Comment