angularjs - router.post returns error "undefined is not a function" .Using mongo and express.js -
i'm trying buld app using files link .i found posting code breaks.has express js changed or syntax mistake ?
the router.post breaks once reaches maid.registermaid(new maid({... .i'm able make work using .save() explain why callback beaking ?
putting code below.. sorry, i'm beginner in m.e.a.n
api.js
var express = require('express'), router = express.router(), passport = require('passport'); user = require('../models/user.js'); maid = require('../models/maid.js'); router.post('/addmaid', function(req, res) { console.log(req.body, req.body.firstname,req.body.lastname); maid.registermaid(new maid({ firstname: req.body.firstname }), ({lastname: req.body.lastname}), function(err, account) { if (err) { return res.status(500).json({err : err}) } return res.status(200).json({status: 'registration successful!'}); });
});
services.js
angular.module('myapp').factory('authservice',['$q', '$timeout', '$http', function ($q, $timeout, $http) { var user = null; return ({ isloggedin: isloggedin, getuserstatus: getuserstatus, login: login, logout: logout, register: register, registermaid: registermaid }); function registermaid(fname, lname) { var deferred = $q.defer(); $http.post('/maid/addmaid', { firstname : fname, lastname : lname }).success(function(data, status) { if (status === 200 && data.status) { deferred.resolve(); } else { deferred.reject(); } }).error(function(data) { debugger; alert("error in services addmaid"); deferred.reject(); }); return deferred.promise; } }]);
controllers.js
angular.module('myapp').controller('addmaidcontroller', ['$scope', '$http','$location', 'authservice', function($scope, $http,$location, authservice) { console.log(authservice.getuserstatus()); $scope.register = function () { $scope.error = false; $scope.disabled = true; authservice.registermaid($scope.registerform.firstname,$scope.registerform.lastname).then(function () { $scope.disabled = false; $scope.registerform = {}; }).catch(function () { $scope.error = true; $scope.errormessage = "something went wrong!"; }); };}]);
maid.js
var mongoose = require('mongoose'), schema = mongoose.schema; var maid = new schema({ first_name: string, last_name: string }); module.exports = mongoose.model('maids', maid);
first of globally defined ur user , maid modules, not practice. error occurs because maid module (maid model on server side, i.e. nodejs) doesnt have registermaid method. need use maid.create
instead
Comments
Post a Comment