Friday 14 April 2017

How can i verify if a user exists in database mongodb using node.js and angular.js

I've just to check if a user alredy exists on my back-end and show in my front-end. I need to create other api function to do this ? Please, help me. I'm a beginner in MEAN apps. It's my controller of my form:

angular.module('sun').controller('UsersController', function($scope, $http, $location){

    $scope.mensage = {};
    $scope.user = {}; 


    $scope.submit = function() { 

        if($scope.form.$valid){

       var promisse = $http.post('/v1/users', $scope.user)

            promisse.then(function(rt){

                $scope.user = rt.data;
                $location.path('/users');
                $scope.mensage = 'Congrats!';
                $scope.user = {};

       }).catch(function(error){
            console.log(error);
            $scope.mensage = 'Failed';
       });

     }             
 };
});

It's my back-end:

 var mongoose = require('mongoose');
 var api = {};
 var model = mongoose.model('User');

api.add = function(req,res){
    var user = request.body;
model
    .create (user)
    .then (function(user){
        response.json(user);
    },function(error){
        console.log(error);
        response.status(500).json(error);  
  });        
};
module.exports = api;



via Douglas Batista

No comments:

Post a Comment