![](/img/trans.png)
[英]Oracle DB select query returning the data in form of array instead of json array using nodejs
[英]select * query returning data from db
我有一個表設置(在我的數據庫中),用戶可以在其中 select 他們想要將 email 發送到哪個類別(從前端)。 我的桌子設置是這樣的:
name
category1
category2
category3
這些都是列。 因此,在這些類別中的每一個列中都是電子郵件。 我基本上想從用戶選擇的任何類別中獲取數據。 但是,我似乎無法獲取該數據。
這是我查詢數據庫的代碼:
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");
});
發送 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);
});
});
})
我從行中得到的結果是
{ CATEGORY1: email1 }
{ CATEGORY1: email2 }
{ CATEGORY1: email3 }
但是當它運行 email 代碼時,我得到了錯誤:
Error: No recipients defined
您需要提取 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");
});
// 使用定義的傳輸 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.