gpt4 book ai didi

mysql - 如何在 javascript (nodejs) 中使用 for 或 foreach 将带有对象值的数组插入到 mysql 中?

转载 作者:行者123 更新时间:2023-11-29 18:07:15 24 4
gpt4 key购买 nike

这是我的 cron 作业 API:

var job = new cronJob('* * * * * *', function () {
Draft.find().then(data => {
var finalData = data;
Profsms.create({
phoneno: draftData.contacts.gsm,
sender: senderName,
message: message
}).then(function (data) {
if (data) {
console.log("successfully moved in profsms mysql");
} else {
console.log("failed");
}
})
});
}, function () {
console.log('DelCron Job finished.');
}, true, 'Asia/Calcutta');

finalData 变量中,我得到了这种类型的 json 对象:

{
"draftType" : "contactdraft",
"scheduledTime" : null,
"senderdata" : "",
"draftData" : {
"contacts" : [
{
"updatedAt" : "2017-12-07T12:09:10.000Z",
"createdAt" : "2017-12-07T12:09:10.000Z",
"data3" : "",
"data2" : "",
"data1" : "",
"country" : "",
"url" : "",
"company" : "",
"email" : "sameer@ech.com",
"dob" : null,
"postcode" : "",
"region" : "",
"city" : "",
"street" : "",
"lastName" : "Sameer",
"firstName" : "Mohamed",
"gsm" : "123344",
"id" : 12
},
{
"updatedAt" : "2017-12-07T12:09:58.000Z",
"createdAt" : "2017-12-07T12:09:58.000Z",
"data3" : "",
"data2" : "",
"data1" : "",
"country" : "",
"url" : "",
"company" : "",
"email" : "ganesh@ich.com",
"dob" : null,
"postcode" : "",
"region" : "",
"city" : "",
"street" : "",
"lastName" : "Pandiyan",
"firstName" : "Ganesh",
"gsm" : "1233",
"id" : 13
}
]
},
"senderName" : "ifelse",
"message" : "hey...",
"draftName" : "December 9",
}

我想将draftData和senderName中的gsm号码以及消息值插入数据库

想要存储

                    phoneno: draftData.contacts.gsm,
sender: senderName,
message: message

如何做到这一点?

我正在使用带有 mysql 的sequelize ORM。

你能帮我使用 for 或 foreach 或 JavaScript 中的任何其他函数来解决这个问题吗?

最佳答案

我终于编写了自己的代码并得到了答案:

var job = new cronJob('* * * * * *', function () {
Draft.find().then(data => {
var finalData = data;
var array = [];
for (var i = 0; i < finalData.length; i++) {
for (var j = 0; j < finalData[i].draftData.contacts.length; j++) {
array.push({
phoneno: finalData[i].draftData.contacts[j].gsm,
sender: finalData[i].senderName,
message: finalData[i].message
})
}
}
Profsms.bulkCreate(array).then(function (data) {
if (data) {
console.log("successfully moved in profsms mysql");
} else {
console.log("failed");
}
})
});
}, function () {
console.log('DelCron Job finished.');
}, true, 'Asia/Calcutta');

这是个好方法吗?

关于mysql - 如何在 javascript (nodejs) 中使用 for 或 foreach 将带有对象值的数组插入到 mysql 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47730644/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com