how to use uuid in backend

 Dependencies used - 




file structure - 





Server.js file - 



const express = require('express')
const {v4 : uuidv4= require('uuid')
const mongoose = require('mongoose')
const record = require('./db')
const bodyparser = require('body-parser')


mongoose.connect('mongodb://localhost:27017/testfifteen',{ useNewUrlParsertrue ,useUnifiedTopologytrue})
.then(()=>console.log("db connected"))
.catch((err)=>console.log(err))

const app = express();

app.use(bodyparser.urlencoded({extended:false}))

app.set('view engine','ejs')

app.get('/',(req,res)=>{
    res.render('index')
})

app.post('/index',async (req,res)=>{
    const newId = uuidv4()
    console.log(newId)
    
    const detail = {
        name req.body.name,
        uid newId
    }
     const data = new record(detail)
    await  data.save()
    res.redirect('/')
})

app.listen('3000')



database file

const mongoose = require('mongoose')

const recordSchema = new mongoose.Schema({
    uid String,
    name String
})


module.exports = mongoose.model('record',recordSchema)

Comments

All time Best Post

Write a program that declares a class named Person. It should have instance variables to record name, age and salary. Use new operator to create a Person object. Set and display its instance variables.

IMAGE CRUD OPERATION USING EXPRESS MONGOOSE MULTER

connect websocket to binance api and store t he true value in the db

CRUD APP WITH SORT IN NODE EXPRESS AND MONGODB

express session login system using node express mongodb

how to delete image from the folder directly from a Request (MULTER)

calculate rsi

java program for 1 to 10 print using while loop

How to fetch last one minute volume of BTC in CCXT Nodejs.