Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

sinha 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 :: ignores _id mongoose schema 
Javascript :: how to write tuples in elixir 
Javascript :: state wheteher true or false The charioteer sprinkled sacred water on the king. 
Javascript :: opal find element 
Javascript :: js get website short name 
Javascript :: Introdução ao nodeJs 
Javascript :: how to use pass value to the function that was called onchange in react 
Javascript :: how to disable all buttons in javascript 
Javascript :: generate secret key js Java Script 
Javascript :: react native navigation back 
Javascript :: creare component in anglar 
Javascript :: remove backslash in json array javascript 
Javascript :: shopping cart small icon code react-bootstrap 4.6 fa fas 
Javascript :: how to communicate between nodejs applications 
Javascript :: js find key by value in object 
Javascript :: how to make a if loop happen one 
Javascript :: generate random brightest color 
Javascript :: render image url in react native 
Javascript :: how to serve build react 
Javascript :: unordered list in react native 
Javascript :: border radius not working ios react native 
Javascript :: Invariant Violation: requireNativeComponent: "RNSScreen" was not found in the UIManager 
Javascript :: check balance of a wallet in js 
Javascript :: autofocus react 
Javascript :: js set attribute 
Javascript :: jquery clone and append 
Javascript :: divide array of objects to 4 arrays js 
Javascript :: what is the difference beetween += and =+ 
Javascript :: node list folders in directory 
Javascript :: load js file 
ADD CONTENT
Topic
Content
Source link
Name
7+8 =