简体   繁体   中英

Data creation in Mongodb using Node JS

I am using node js as my backend language. I am creating data in mongodb through submitting data using a form but I want to find the latest entered data and display it in my ejs template.

Here is my code:

/*mongoose schema setup*/
const codeSchema = new mongoose.Schema({
   first: String,
   second: String,
   third: String,
   event: String,
   link: String
});

const Code = mongoose.model('Code', codeSchema);

/* GET home page. */
router.get('/welcome', async function(req, res, next) {             
  await Code.find({}).sort({_id: -1}).exec(function(err, data) {   
        if(err) 
        {
            console.log(err); 
        }   
        else
        {
      //console.log(data);
      res.render('index', {data: data});
        }   
  });
});

/* GET new form form page. */
router.get('/update', function(req, res, next) {
  res.render('newform');
});

/* POST update welcome page. */
router.post('/welcome', async function(req, res, next) {
  const info = {
    first: req.body.first,
    second: req.body.second,
    third: req.body.third,
    event: req.body.event,
    link: req.body.link
  }; 
  await Code.create(info, function(err){
    if(err) {
      console.log(err);
    }
    else {
      res.redirect('/welcome');

     }
   });
 });

The code works fine, it creates and extracts the last n record from my database but when I try to display the data in my html it does'nt shows up.I also tried using findOne() method which actually worked and displayed the last n record from my db. The problem arised when my database had no data the findOne() did'nt worked and generated error in my ejs template.

I find the data from my db using find({}) method. Like if I do First name - <%= data.first %> in my ejs template, my data does'nt show. I also checked my mongo database which had all the information which was passed through form. PLEASE HELP !!!!!

PS - I can display all the data from db using for loop but I only want to display a particular data.

Nithin K Joy here is my index.ejs

<h1>Index Page</h1>
<h4> Person1- <%= data.first %> <h4/>
<h4> Person2- <%= data.second %> <h4/>
<h4> Person3- <%= data.third %> <h4/>
<h4> Event name- <%= data.event %> <h4/>
<h4> Registration Link- <%= data.link %> <h4/>

here is my form through which recors are being submitted

<form action="/welcome" method="POST" style="width: 30%; margin: 40px auto;">
<input type="text" id="winner1" name="first" placeholder="1st">
<input type="text"  id="winner2" name="second" placeholder="2nd" >
 <input type="text" id="winner3" name="third" placeholder="3rd" >
<input type="text" id="event" name="event" placeholder="Event Name" >
<input type="text" id="link" name="link" placeholder="Event Regestration link" >
<button type="submit class="btn btn-success btn-block">Submit</button>

If i put my index.ejs code inside a loop it works fine but I donot want several inputs from FORM to show up in index.ejs I want only the last n record

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM