简体   繁体   English

select * 查询从数据库返回数据

[英]select * query returning data from db

I have a table set up (in my db) where a user can select which category they want to send an email to (from front end).我有一个表设置(在我的数据库中),用户可以在其中 select 他们想要将 email 发送到哪个类别(从前端)。 my table set up is like this:我的桌子设置是这样的:

name
category1
category2
category3

those are all columns.这些都是列。 So in each of those category columns are emails.因此,在这些类别中的每一个列中都是电子邮件。 I basically want to grab the data from whichever category the user selects.我基本上想从用户选择的任何类别中获取数据。 However, I cannot seem to grab that data.但是,我似乎无法获取该数据。

here's my code to query the db:这是我查询数据库的代码:

app.get('/sendBatchCategory', (req, res) => {
var category = req.query.category
category = category.toUpperCase();
var subject = req.query.subject;
var message = req.query.message;

var categoryQuery = ""
if (category == "CATEGORY1") {
    categoryQuery = "SELECT CATEGORY1 FROM EMAILS"
} else if (category == "CATEGORY2") {
    categoryQuery = "SELECT CATEGORY2 FROM EMAILS"
} else if (category == "CATEGORY3") {
    categoryQuery = "SELECT CATEGORY3 FROM EMAILS"
} else if (category == "ALL") {
    categoryQuery = "SELECT CATEGORY1, CATEGORY2, CATEGORY3 FROM EMAILS"
} else {
    alert("category not found, please select a valid category")
}
//"SELECT '" + category + "' FROM EMAILS"
var sendBatchCategory = categoryQuery
ibmdb.open(ibmdbconnDash, function (err, conn) {
    if (err) return console.log(err);
    conn.query(sendBatchCategory, function (err, rows) {
      if (err) {
        console.log(err);
      }

      console.log(rows)
      var listOfEmails = (rows) // here is where I am trying to store the data being called          into a variable to pass into the code to send an email.

      conn.close(function () {
        console.log("closing function sendBatchCategory");
      });

code to send an email (all within same function)发送 email 的代码(都在同一功能内)

     // async..await is not allowed in global scope, must use a wrapper
      async function main() {
        // Generate test SMTP service account from ethereal.email
        // Only needed if you don't have a real mail account for testing
        let testAccount = await nodemailer.createTestAccount();
    
        // create reusable transporter object using the default SMTP transport
        let transporter = nodemailer.createTransport({
          host: "smtp.gmail.com",
          port: 587,
          secure: false, // true for 465, false for other ports
          auth: {
            user: "x",
            pass: "y",
          },
        });
    // send mail with defined transport object
    let info = await transporter.sendMail({
      from: "x", // sender address
      to: listOfEmails, // list of receivers
      subject: subject, // Subject line
      text: message //message line
    });

    sendBatchSuccess(req, res)

    console.log("Message sent: %s", info.messageId);
    // Message sent: <b658f8ca-6296-ccf4-8306-87d57a0b4321@example.com>

    // Preview only available when sending through an Ethereal account
    console.log("Preview URL: %s", nodemailer.getTestMessageUrl(info));
    // Preview URL: https://ethereal.email/message/WaQKMgKddxQDoou...
  }

  main().catch(console.error);
 
        });
    });
})

the result that I get from the rows is我从行中得到的结果是

{ CATEGORY1: email1 }
{ CATEGORY1: email2 }
{ CATEGORY1: email3 }

but when it runs the email code, I get the error:但是当它运行 email 代码时,我得到了错误:

Error: No recipients defined

you need to extract the email list您需要提取 email 列表

ibmdb.open(ibmdbconnDash, function (err, conn) {
    if (err) return console.log(err);
    conn.query(sendBatchCategory, function (err, rows) {
      if (err) {
        console.log(err);
      }

      console.log(rows);
      var listOfEmails =[];
      rows.foreach(function(entry){
         if(entry.hasOwnProperty('CATEGORY1')){
              listOfEmails.push(entry.CATEGORY1);
         }
        if(entry.hasOwnProperty('CATEGORY2')){
              listOfEmails.push(entry.CATEGORY2);
         }
         if(entry.hasOwnProperty('CATEGORY3')){
              listOfEmails.push(entry.CATEGORY3);
         }
      })
      conn.close(function () {
        console.log("closing function sendBatchCategory");
      });

// send mail with defined transport object // 使用定义的传输 object 发送邮件

    let info = await transporter.sendMail({
      from: "x", // sender address
      to: listOfEmails.join(","), // list of receivers
      subject: subject, // Subject line
      text: message //message line
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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