javascript - Simple web SPA with API and MONGOOSE (issue with id's) -
i creating simple anjularjs spa connecting mongoose via api. app take in new member, display new member , if click on members name in list should able edit member in screen.
but when click on members name edit it not give me member on page edit, cannot specify correct member edit. when hover on member name expect see id mongoose member instead see:
localhost:4000/#/members/ (i think should seeing (with id mongoose)???? : localhost:4000/#/members/553778bb92de243be1c61c6d )
where going wrong index's / id's??
here members.html page showing list of members, should able move new page when click members name edit member can't. see "{{memb.name | uppercase}}" below...
members.html
<div class="col-sm-7 col"> <div class="csstablegenerator" > <h2 align="center">registered club members</h2> <table> <tr><td>name</td><td>address</td><td>age</td><td>level</td><td>able swim</td><td>email</td><td>registration date</td></tr> <tr ng-repeat="memb in members" > <td> <a href="#/members/{{member._id}}">{{memb.name | uppercase}} </a> </td> <td> {{memb.address }} </td> <td> {{memb.age }} </td> <td> {{memb.level }} </td> <td> {{memb.swimmer }} </td> <td> {{memb.email }} </td> <td> {{memb.regdate }} </td> </tr> </table> </div> </div>
my app.js:
var app = angular.module("myapp",['ngroute']) app.config(function ($routeprovider) { $routeprovider .when('/members', { controller: 'memberscontroller', templateurl: './partials/members.html' }) .when('/members/:member_index', { controller: 'memberdetailcontroller', templateurl: './partials/member_edit.html' }) .when('/home', { //controller: 'memberscontroller', templateurl: './partials/home.html' }) .otherwise({ redirectto: '/home' }); }) app.factory('simplefactory', ['$http', function($http){ var factory = {}; var members = $http.get('/api/members') factory.getmembers = function () { //return members; return members = $http.get('/api/members'); } factory.getmember = function (member_id) { return members[member_id] = $http.get('/api/members/' + member_id ) } factory.addmember = function(member) { return $http.post('/api/members',member) } factory.updatemember = function(member_id,member) { return $http.put('/api/members/' + member_id, member) } return factory; }]) app.controller('memberscontroller', ['$scope','simplefactory', function ($scope,simplefactory) { simplefactory.getmembers() .success(function(members) { $scope.members = members; }); $scope.addmember = function() { var member = { name: $scope.newmember.name, address: $scope.newmember.address, age : $scope.newmember.age, level : $scope.newmember.level, swimmer : $scope.newmember.swimmer, email : $scope.newmember.email, regdate : $scope.newmember.regdate, } simplefactory.addmember(member) .success(function(added_member) { $scope.members.push(added_member); $scope.newmember = { } } ); } }]) app.controller('memberdetailcontroller', ['$scope','$location','$routeparams','simplefactory', function ($scope,$location,$routeparams,simplefactory) { simplefactory.getmember($routeparams.member._id).then(function(data){ $scope.member = { index : $routeparams.member._id, detail : data.user }; }); $scope.updatemember = function() { simplefactory.updatemember($scope.member._id, $scope.member.detail) .then(function(data) { $location.path('/members') }); }; }])
my index.js
var express = require('express'); var controller = require('./members.controller'); var router = express.router(); router.get('/', controller.index); router.get('/:id', controller.show); router.post('/', controller.create); router.put('/:id', controller.update); module.exports = router;
my members.model.js
var mongoose = require('mongoose') var schema = mongoose.schema;
var memberschema = new schema({ name: string, address: string, age: string, level: string, swimmer: string, email: string, regdate: string }); module.exports = mongoose.model('members', memberschema);
my members.controller.js
var _ = require('lodash') var member = require('./member.model'); function handleerror(res, err) { return res.send(500, err); } // list of members exports.index = function(req, res) { member.find(function (err, members) { if(err) { return handleerror(res, err); } return res.json(200, members); }); } ; // creates new member in datastore. exports.create = function(req, res) { member.create(req.body, function(err, member) { if(err) { return handleerror(res, err); } return res.json(201, member); }); }; exports.show = function(req, res) { member.findbyid(req.params.id, function (err, member) { if(err) { return handleerror(res, err); } return res.json(200, member); }); } ; // update existing member in datastore. exports.update = function(req, res) { member.findbyid(req.params.id, function (err, member) { member.name = req.body.name member.address = req.body.address member.age = req.body.age member.level = req.body.level member.swimmer = req.body.swimmer member.email = req.body.email member.regdate = req.body.regdate member.save(function (err) { if(err) { return handleerror(res, err); } return res.send(200, 'update successful'); }); }); }
can this? simple app learning , stuck here @ moment!
thanks
your a
tag in members.html should bind memb._id
, not members._id
. this:
<a ng-href="#/members/{{memb._id}}">{{memb.name | uppercase}}</a>
Comments
Post a Comment