Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

crud template

async function run(){
    try{
        await client.connect();
        const userCollection = client.db('foodExpress').collection('user');

        // get users
        app.get('/user', async(req, res) =>{
            const query = {};
            const cursor = userCollection.find(query);
            const users = await cursor.toArray();
            res.send(users);
        });

        app.get('/user/:id', async(req, res) =>{
            const id = req.params.id;
            const query = {_id: ObjectId(id)};
            const result = await userCollection.findOne(query);
            res.send(result);
        });

        // POST User : add a new user
        app.post('/user', async(req, res) =>{
            const newUser = req.body;
            console.log('adding new user', newUser);
            const result = await userCollection.insertOne(newUser);
            res.send(result)
        });

        // update user
        app.put('/user/:id', async(req, res) =>{
            const id = req.params.id;
            const updatedUser = req.body;
            const filter = {_id: ObjectId(id)};
            const options = { upsert: true };
            const updatedDoc = {
                $set: {
                    name: updatedUser.name,
                    email: updatedUser.email
                }
            };
            const result = await userCollection.updateOne(filter, updatedDoc, options);
            res.send(result);

        })

        // delete a user
        app.delete('/user/:id', async(req, res) =>{
            const id = req.params.id;
            const query = {_id: ObjectId(id)};
            const result = await userCollection.deleteOne(query);
            res.send(result);
        })

    }
    finally{

    }
}

run().catch(console.dir);
Comment

PREVIOUS NEXT
Code Example
Javascript :: vue add external script 
Javascript :: open new window javascript 
Javascript :: bitfield permissions discord,.js 
Javascript :: get selector with specific text puppeteer 
Javascript :: json full form 
Javascript :: javascript detect scroll wheel 
Javascript :: networkx check if node exists 
Javascript :: jquery each hover 
Javascript :: js wait until 2 promises are resolved 
Javascript :: socket io query 
Javascript :: window.addEventListener("online"); 
Javascript :: mongoose connect 
Javascript :: javascript require 
Javascript :: javascript add an element to an array 
Javascript :: mongoose find multiple and update 
Javascript :: javascript to array 
Javascript :: axios get method 
Javascript :: magento 2 translate js 
Javascript :: node-json-db 
Javascript :: bson to json converter 
Javascript :: js use restrict 
Javascript :: javascript The replace() method 
Javascript :: what is the function of delete operator in javascript 
Javascript :: java convert json string to list of maps 
Javascript :: nuxt add plugin 
Javascript :: react.createelement 
Javascript :: mongodb empty an array field 
Javascript :: reverse array js 
Javascript :: get all date between two dates in javascript 
Javascript :: disable other options in select except the selected 
ADD CONTENT
Topic
Content
Source link
Name
9+5 =