首页 > 解决方案 > 我有数组对象,想验证一些文件,如电子邮件、联系人编号

问题描述

我正在从req.body(数组对象数据)获取输入数据并尝试验证某些文件,例如email, contactno,但我不知道为什么我无法验证这些字段。

我尝试了各种方法,但没有成功。

请检查我的代码并尽快回复我必须尽快完成

 addTickets : async function(req,res){
        var data = [req.body];
        var eventid = req.body.eventid;
        var catid = req.body.ticketcategoryid; 
        var num = Math.floor(Math.random() * 90000) + 10000;
        event_data = await Event.findOne({
            where:{
              id:eventid
            }
          });
        if(event_data){
          ticket_data = await Tickets.findOne({
            where:{
              id:catid
            }
          });
          if(ticket_data){
            req.check('email').isEmail().withMessage('Please enter valid email');
            req.check('contactno').notEmpty().withMessage('Please enter your contact no');
            var error = req.validationErrors();
            if (error) {
              return  fun.returnResponse(res,false,400,fun.checkRequired(error).message);
              return false;
            }
            else{
                
                await con.query('INSERT INTO table SET ?', data, function (error, results, fields) {
                if (error) throw error;
                var msg = {
                  from: "abc@gmail.com", // sender address
                  subject: "Hello ✔", // Subject line
                  text: "Hello This is an auto generated Email for testing  from node please ignore it  ✔", // plaintext body
                  cc: "*******"    
                  //  html: "<b>Hello world ✔&lt;/b>" // html body
                }
                var maillist = [
                        'xyz@gmail.com',
                        'a.bussa@gmail.com',
                        'b.gawri@gmail.com',
                      ];
                     // var sendto = data.email;
    
                      maillist.forEach(function (to, i , array) {
                        msg.to = to;
    
                        transporter.sendMail(msg, function (err) {
                          if (err) { 
                            console.log('Sending to ' + to + ' failed: ' + err);
                            return;
                          } else { 
                            console.log('Sent to ' + to);
                          }
                        res.end(JSON.stringify(results));
    
                      });
                    });
                  });
                }
              }
              else{
                 return  fun.returnResponse(res,false,204,"Ticket not found");
              return false;
              }
            }
            else{
              return  fun.returnResponse(res,false,204,"Event not found");
              return false;
            }
          //console.log(ticket_data);  
      }

标签: mysqlnode.jsarraysvalidation

解决方案


推荐阅读