Insert data in mongoose using a factory

后端 未结 1 353
灰色年华
灰色年华 2021-01-25 05:14

I am trying to insert data in mongoose using angularjs and node js. For this i have created a factory where i am calling another js file where i have created my database connect

相关标签:
1条回答
  • 2021-01-25 05:53

    You cannot inject $scope to factory. Services do not have scopes. Only Controllers.

    So you have to use something like this.

    test.factory('registrationservice', function($http){
        var factory = {};
        // if user is another service you have to inject it in factory defenotion function
        // and delete from here.
        facotry.newregister = function(user, $scope) {
    
            var newUser = new user({
                username: $scope.uName,
                firstname: $scope.fName,
                lastname: $scope.lName,
                email:$scope.mail,
                password: $scope.newpwd
    
            });
    
            return $http.post('data/registration.js', newUser);
        }
        return factory;
    });
    

    And then in your controller.

    test.controller('registrationCtrl', function($scope, $log, registrationservice){ 
        registrationservice.newregister(user, $scope).success(function(msg){
            $log.info(msg.data);
        })
    });
    
    0 讨论(0)
提交回复
热议问题