I use NodeJS to insert data to a table with many to many relationship, and I want to include two foreign keys when I insert data into the table this is how my code looks:
我使用NodeJS将数据插入到具有多对多关系的表中,并且我想在将数据插入表中时包含两个外键,这就是我的代码的外观:
con.query("SELECT * FROM Transaction WHERE TransactionID > 1", function(err, res) {
if (err) {
throw (err);
} else if (res.length > 0) {
console.log("Transaction already exit");
} else {
var transactionID;
var filePK;
con.query("SELECT Filename FROM File WHERE Filename = ?", fileName, function(err, res) {
if (err) throw err;
filePK = JSON.stringify(res);
});
con.query("SELECT TransactionDescriptionPK FROM TransactionDescription WHERE TransactionDescriptionPK > 0", function(err, res) {
if (err) throw err;
//console.log(res);
transactionID = res;
});
var tran = {
TransactionID: data.ID,
TransactionDate: data.Description,
Amount: data.Amount
};
con.query("INSERT INTO Transaction SET ?", tran, function(err, res) {
if (err) throw err;
});
How I return the results from those queries so I can add them to the tran object?
我如何从这些查询中返回结果,以便将它们添加到tran对象中?
1 个解决方案
#1
0
You can use async.parallel
您可以使用async.parallel
async.parallel([
function(callback) {
// you can directly pass the parallel callback to mysql query
// if you don't need to do anything else
return con.query(
"SELECT * FROM Transaction WHERE TransactionID > 1",
callback
);
},
function(callback) {
// otherwise, just do you what you want (here JSON.stringify)
// then don't forget to return the parallel callback
return con.query(
"SELECT Filename FROM File WHERE Filename = ?",
fileName,
function(err, res) {
if (err)
return callback(err);
const filePK = JSON.stringify(res);
return callback(null, filePK);
}
);
},
function(callback) {
return con.query(
"SELECT TransactionDescriptionPK FROM TransactionDescription WHERE TransactionDescriptionPK > 0",
callback
);
},
], function(err, data) {
// if any mysql queries above encounter an error,
// it calls the parallel final callback and stops other functions
// without errors, data looks like :
// data[0] equals mysql result object of the first query
// data[1] equals filePK const
// data[2] equals mysql result object of the last query
const tran = {
TransactionID: data[0][0].ID,
TransactionDate: data[0][0].Description,
// Amount: data.Amount // don't know where the amount come from,
// but you get the idea
};
con.query(
"INSERT INTO Transaction SET ?",
tran,
function(err, res) {
if (err)
throw err;
// ...
}
);
});
#1
0
You can use async.parallel
您可以使用async.parallel
async.parallel([
function(callback) {
// you can directly pass the parallel callback to mysql query
// if you don't need to do anything else
return con.query(
"SELECT * FROM Transaction WHERE TransactionID > 1",
callback
);
},
function(callback) {
// otherwise, just do you what you want (here JSON.stringify)
// then don't forget to return the parallel callback
return con.query(
"SELECT Filename FROM File WHERE Filename = ?",
fileName,
function(err, res) {
if (err)
return callback(err);
const filePK = JSON.stringify(res);
return callback(null, filePK);
}
);
},
function(callback) {
return con.query(
"SELECT TransactionDescriptionPK FROM TransactionDescription WHERE TransactionDescriptionPK > 0",
callback
);
},
], function(err, data) {
// if any mysql queries above encounter an error,
// it calls the parallel final callback and stops other functions
// without errors, data looks like :
// data[0] equals mysql result object of the first query
// data[1] equals filePK const
// data[2] equals mysql result object of the last query
const tran = {
TransactionID: data[0][0].ID,
TransactionDate: data[0][0].Description,
// Amount: data.Amount // don't know where the amount come from,
// but you get the idea
};
con.query(
"INSERT INTO Transaction SET ?",
tran,
function(err, res) {
if (err)
throw err;
// ...
}
);
});