Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
83 views
in Technique[技术] by (71.8m points)

node.js - How can i handle assertion error in mogodb+Nodejs?

mongo.connect('mongodb://localhost',{useUnifiedTopology: true}).then((client) => {

        var db = client.db('complainbox');
        
                db.collection('admin').findOne({"Email":req.body.Email},(err,result)=>{
                   assert.equal(null,err);
                   assert.equal(req.body.Email,result.Email);
                   
                   assert.equal(req.body.Password,result.Password);
                   
                   console.log("found!!!!"+ result._id);
                   session.adminId=result._id;
                   
                   
                   res.redirect('/addstudent');
                   client.close();
                   
               });
         
           console.log('DB Connecmted!')
        }).catch(err => {
            
            
           // console.log(err);

        });

Err for wrong password

throw err;
      ^

AssertionError [ERR_ASSERTION]: 'wed' == '123456'
    at db.collection.findOne (/home/katha/Documents/newweb/app.js:217:27)
    at executeCallback (/home/katha/Documents/newweb/node_modules/mongodb/lib/operations/execute_operation.js:70:5)
    at handleCallback (/home/katha/Documents/newweb/node_modules/mongodb/lib/utils.js:102:55)
    at cursor.next (/home/katha/Documents/newweb/node_modules/mongodb/lib/operations/find_one.js:29:9)
    at /home/katha/Documents/newweb/node_modules/mongodb/lib/utils.js:677:5
    at cursor._next (/home/katha/Documents/newweb/node_modules/mongodb/lib/cursor.js:250:9)
    at handleCallback (/home/katha/Documents/newweb/node_modules/mongodb/lib/core/cursor.js:31:5)
    at nextFunction (/home/katha/Documents/newweb/node_modules/mongodb/lib/core/cursor.js:864:5)
    at self._initializeCursor (/home/katha/Documents/newweb/node_modules/mongodb/lib/core/cursor.js:740:7)
    at done (/home/katha/Documents/newweb/node_modules/mongodb/lib/core/cursor.js:458:7)
[nodemon] app crashed - waiting for file changes before starting...

How can i handle it??

question from:https://stackoverflow.com/questions/65651281/how-can-i-handle-assertion-error-in-mogodbnodejs

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

My problem was solved using this approah

            db.collection('admin').findOne({"Email":req.body.Email},(err,result)=>{
              try{ assert.equal(null,err);
               assert.equal(req.body.Email,result.Email);
               
               assert.equal(req.body.Password,result.Password);
               
               console.log("found!!!!"+ result._id);
               session.adminId=result._id;
               
               
               res.redirect('/addstudent');
               client.close();
              }
              catch(e){
                session.studentId=null;
                session.studentname = null;
                session.registration=null;
                session.Studentemail=null;
                session.adminId=null;
                  console.log(e);
                  res.redirect('/admin');
              }
               
           })
       

        
       

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...