Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

nodejs curd insert update delete


///////////////Find all Data


app.get('/api/',async(req, res) =>{
     const result = await  users.findAll();
     res.send(result);
})


app.get('/api/findbyid/:id',async(req, res) =>{
    const result = await  users.findOne({
        where:{id: req.params.id}
    });
    res.send(result);
})


app.get('/api/findone/:name',async(req, res) =>{
  //  const name = req.body.name;
  //console.log(req.params.name);
  const name= req.params;
    const result = await  users.findAll({
        where:{name: name}
    });
    res.send(result);
})


///////////////    insert new users

app.post('/api/insert/',async (req, res) =>{
    const id = req.params.id;
    const result= await users.upsert(
     {name:req.body.name,
     displayName:req.body.displayName},
      // {
      //   where: { id: id }
      // }
    );
    res.send(result);
  
/////////////   update data 

})
app.put('/api/update/:id',async (req, res) =>{
  const data = req.body.name;              /////fetch 'name' from db
  const display = req.body.displayname;    ////fetch 'displayname'
  //  const id = req.params.id; 
     users.update(
     {name:data,
     displayName:display},
      {
        where: { id: req.params.id }
      }
    );
   // res.send(result);
    res.redirect('/api');
  
})


////////////////// Delete data from database

app.delete('/api/delete/:id',async (req, res)=>{
  const result = await users.destroy({
        where: {id: req.params.id}
    })
   // res.send(result);
    res.redirect('/api')
})

Comment

PREVIOUS NEXT
Code Example
Javascript :: Recorrer Array con forEach 
Javascript :: getinitialprops to a hoc in next js 
Javascript :: how to format a javascript date 
Javascript :: simultaneos mouse buttons clicked js 
Javascript :: using while loop to create table rows js 
Javascript :: pdf.js get current page number 
Javascript :: hide playback speed from videojs 
Javascript :: nodejs export all mongodb collections 
Javascript :: How to write on a web page javascript 
Javascript :: how to add debounce in react redux js 
Javascript :: JavaScript: Updating Object Properties 
Javascript :: mongodb find array with element 
Javascript :: Event Custom Fire 
Javascript :: link externo no react 
Javascript :: comment faire pour écrire un texte en javascript 
Javascript :: generate string from regex javascript 
Javascript :: How to find out what character key is pressed?#key#keyCode#code 
Javascript :: opposite number js 
Javascript :: confirm closing tab 
Javascript :: get list of filenames in folder 
Javascript :: grapesjs cdn 
Javascript :: emergency food 
Javascript :: js replace text link with anchor tags 
Javascript :: sitecore rendering paramaters get 
Javascript :: react linking to documents 
Javascript :: jest mock call 
Javascript :: axios httsagent 
Javascript :: react clear input after button click 
Javascript :: strapi blank dashboard page 
Javascript :: scroll to top vue 
ADD CONTENT
Topic
Content
Source link
Name
1+1 =