Node JS

Node js Angular Mongodb Search Functionality

In this articles will teach how how to search records Node js Angular Express Mongodb.how the search functionality works.

Create the Node js Project using following command

npm init

Install the following dependencies

  • npm i mongoose
  • npm i express
  • npm install –save-dev nodemon

Server.js

const express = require('express')
const sever = express()
const port = 4000
var routes = require('./routes/routes');
var mongoose = require('mongoose');
mongoose.set('strictQuery', true);
mongoose.connect("mongodb://127.0.0.1:27017/dbschool",{useNewUrlParser: true,  useUnifiedTopology: true },function checkDb(error)
{
    if(error)
    {
        console.log(error);
    }
    else
    {
        console.log("successfully Connected to DB");
    }
});

sever.use(express.json());
sever.use(routes);
sever.listen(port, () => {
  console.log(`Express port successfully ${port}`)
})

routes.js

var express = require('express');
var userController = require('../src/employee/userController');

const router = express.Router();

router.route('/user/findOne/:name').get(userController.findOneUserController);

module.exports = router;

userController.js

var userService = require('./userService');

var findOneUserController = async (req, res) => 
{  
    console.log(req.params.name );
    var result = await userService.findOneUserDBService(req.params.name );

    if (result) {
        res.send({ "status": true, "data": result} );
    } else {
        res.send({ "status": false, "data": "User not found" });
    }
}

module.exports = { findOneUserController};

userModel.js

var mongoose = require('mongoose');
var Schema = mongoose.Schema;

var userSchema = new Schema({

    name: {
        type: String,
        required: true
    },
    address: {
        type: String,
        required: true
    },
    phone: {
        type: String,
        required: true
    }
   

});

module.exports = mongoose.model('employees', userSchema);

userService.js

var userModel = require('./userModel');

module.exports.findOneUserDBService = (userDetais) => {
    return new Promise(function myFn(resolve, reject) {
        userModel.findOne({name:userDetais}, function returnData(error, result) {
          if(error)
          {
                reject(false);
          }
          else
          {
             resolve(result);
          }
        });
 
    });
 
 }

Installing Angular CLI

npm install -g @angular/cli
admin

Recent Posts

Hotel Management System using Laravel 11

Relationships: Hotel ↔ Rooms (One-to-Many) A hotel can have many rooms, but a room belongs…

3 weeks ago

Creating Grocery Inventory App Using React

Introduction to Grocery Inventory Apps Managing grocery inventory can be a daunting task, but with…

4 weeks ago

Fish Inventory Shop Management System in Angular

This article explain how to make a Fish Inventory Management App in Angular.this app explain…

1 month ago

Fish Inventory Management with React

Introduction to Fish Inventory Management In the aquaculture industry, managing fish inventory is crucial for…

1 month ago

Java GUI CRUD for Beginners

Introduction to Java GUI CRUD Java is a powerful programming language widely used for building…

1 month ago

Creating Beautiful Login Form Design Using React

Introduction to Login Form Design Designing an effective and beautiful login form is crucial for…

1 month ago