router.post返回错误" undefined不是函数" 。使用mongo和express.js

时间:2016-01-16 09:28:33

标签: angularjs node.js mongodb express mongoose

enter image description here我试图使用来自LINK的文件来删除应用程序。我发现发布是代码中断的地方。有快递js更改还是语法错误?

一旦到达Maid.registerMaid(新的女佣({...我能够使用.save()使其工作),router.post就会中断但是有人可以解释为什么这个回调正在吵架吗?

将代码放在下面..抱歉,我是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); 

1 个答案:

答案 0 :(得分:0)

首先,您全局定义了用户和女佣模块,这不是一个好习惯。 发生错误是因为您的Maid模块(服务器端的Maid模型,即Nodejs)没有registerMaid方法。您需要使用Maid.create代替