从另一个 table 检索数据并将其插入新的 sequelize
Retriving data from another table and inserting it into new one sequelize
我一直在做一个涉及 sequelize(MySQL DB) 的项目。
所以我有一个 table“InterestRate”,它从另外两个 tables(外键)中获取 ID。
bankId 存储在“Banks”table 中,interId 存储在另一个名为“Interest”的 table 中。
现在我想通过发送此数据用 Postman 填充此 table:
{
"bank": "BankName",
"inter": "Interest",
"nks": "4.11",
"eks": "6.24",
"time": "36"
}
BUT 我想用这些值的主键填充 table(如果存在于它们自己的 table 中)。例如,当我发送给邮递员时,我想检查 table“Banks”并搜索“BankName”,在那个 table 中获取它的 ID,然后把它放在新的 table 中。对于这个内部事物也是如此。
我的代码是垃圾,我知道为什么它不起作用,但我真的被卡住了。
(req, res) => {
const bank = req.body.bank;
const type = req.body.type;
const nks = req.body.nks;
const eks = req.body.eks;
const time = req.body.time;
InterestRate.create({
bankId: bank,
interId: type,
NKS: nks,
EKS: eks,
time: time,
})
.then(() => {
res.status(200).json({ message: 'Added successfully' });
})
.catch((err) => {
res.status(500).send('Error -> ' + err);
});
};
请注意,所有模型等都已正确设置,如果我手动输入内容就可以了!
谢谢!
您只需要通过名称获取 Bank
和 Interest
并使用找到的模型实例获取它们的 ID 来创建 InterestRate
记录:
async (req, res) => {
const bank = req.body.bank;
const type = req.body.type;
const nks = req.body.nks;
const eks = req.body.eks;
const time = req.body.time;
const foundBank = await Bank.findOne({
where: {
name: bank // use a real field name instead of "name"
}
})
const foundInterest = await Interest.findOne({
where: {
name: type // use a real field name instead of "name"
}
})
if(!foundBank) {
// here should be some "res.status(...).send(...)" with error message
return
}
if(!foundInterest) {
// here should be some "res.status(...).send(...)" with error message
return
}
try {
await InterestRate.create({
bankId: foundBank.id,
interId: foundInterest.id,
NKS: nks,
EKS: eks,
time: time,
})
res.status(200).json({ message: 'Added successfully' });
catch (err) {
res.status(500).send('Error -> ' + err);
}
};
我一直在做一个涉及 sequelize(MySQL DB) 的项目。
所以我有一个 table“InterestRate”,它从另外两个 tables(外键)中获取 ID。
bankId 存储在“Banks”table 中,interId 存储在另一个名为“Interest”的 table 中。 现在我想通过发送此数据用 Postman 填充此 table:
{
"bank": "BankName",
"inter": "Interest",
"nks": "4.11",
"eks": "6.24",
"time": "36"
}
BUT 我想用这些值的主键填充 table(如果存在于它们自己的 table 中)。例如,当我发送给邮递员时,我想检查 table“Banks”并搜索“BankName”,在那个 table 中获取它的 ID,然后把它放在新的 table 中。对于这个内部事物也是如此。
我的代码是垃圾,我知道为什么它不起作用,但我真的被卡住了。
(req, res) => {
const bank = req.body.bank;
const type = req.body.type;
const nks = req.body.nks;
const eks = req.body.eks;
const time = req.body.time;
InterestRate.create({
bankId: bank,
interId: type,
NKS: nks,
EKS: eks,
time: time,
})
.then(() => {
res.status(200).json({ message: 'Added successfully' });
})
.catch((err) => {
res.status(500).send('Error -> ' + err);
});
};
请注意,所有模型等都已正确设置,如果我手动输入内容就可以了! 谢谢!
您只需要通过名称获取 Bank
和 Interest
并使用找到的模型实例获取它们的 ID 来创建 InterestRate
记录:
async (req, res) => {
const bank = req.body.bank;
const type = req.body.type;
const nks = req.body.nks;
const eks = req.body.eks;
const time = req.body.time;
const foundBank = await Bank.findOne({
where: {
name: bank // use a real field name instead of "name"
}
})
const foundInterest = await Interest.findOne({
where: {
name: type // use a real field name instead of "name"
}
})
if(!foundBank) {
// here should be some "res.status(...).send(...)" with error message
return
}
if(!foundInterest) {
// here should be some "res.status(...).send(...)" with error message
return
}
try {
await InterestRate.create({
bankId: foundBank.id,
interId: foundInterest.id,
NKS: nks,
EKS: eks,
time: time,
})
res.status(200).json({ message: 'Added successfully' });
catch (err) {
res.status(500).send('Error -> ' + err);
}
};