使用基于单选按钮值的javascript(NodeJS)将数据插入mysql(Sequelize)中 [英] Insert datas into mysql(Sequelize) using javascript(NodeJS) based on radio button value

查看:61
本文介绍了使用基于单选按钮值的javascript(NodeJS)将数据插入mysql(Sequelize)中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在json对象下面有这个

I have this below json object

{
    "phoneno": [
    {
        "field1": "Mohamed",
        "field2": "123456789",
        "field3": "Sameer"
    },
    {
        "field1": "Ganesh",
        "field2": "987654321",
        "field3": "Pandiyan" 
     }
  ],
    "sender": "ifelse",
    "message": "test"
}

我正在使用 ANGULARJS ng-repeat在UI中显示此JSON,也正在动态显示单选按钮,请参见下图

I am showing this JSON in UI using ANGULARJS ng-repeat, also I am displaying radio button dynamically, see the below image

看到选中了单选按钮,在此列中,我有两个手机号码, 所以我要使最终的对象像这样:

see there is radio button checked, in this columns, I have two mobile numbers, so I want to make the final object like this:

[
  {"phoneno" : 123456789 ; "sender" : "ifelse" ; "message" : "test"},
  {"phoneno" : 987654321 ; "sender" : "ifelse" ; "message" : "test"}
]

例如,如果我得到了这种类型的JSON:

For example, if I got this type of JSON:

{
    "phoneno": [
    {
        "field1": "123456789",
        "field2": "Mohamed",
        "field3": "Sameer"
    },
    {
        "field1": "987654321",
        "field2": "Ganesh",
        "field3": "Pandiyan" 
     }
  ],
    "sender": "ifelse",
    "message": "test"
}

再来一次

我正在使用ng-repeat在UI中显示此JSON,也正在动态显示单选按钮,请参见下图

I am showing this JSON in UI using ng-repeat, also I am displaying radio button dynamically, see the below image

看到选中了单选按钮,在此列中,我有两个手机号码, 所以我想使最终的对象像这样:

see there is radio button checked, in this columns, i have two mobile numbers, so I want to make the final object like this:

[
  {"phoneno" : 123456789 ; "sender" : "ifelse" ; "message" : "test"},
  {"phoneno" : 987654321 ; "sender" : "ifelse" ; "message" : "test"}
]

如果我有这种类型的对象,则可以在sequelize的帮助下将其批量插入mysql中.

if i got this type of object, I can bulk insert into mysql with the help of sequelize.

如何做到这一点?

我的api.js(我仍然没有编写代码,正在寻找解决方案)

my api.js ( i did not still code, looking for the solution)

exports.sendFile2Sms = function (req, res) {

};

推荐答案

这将对您有所帮助,在您的数据中,您应该提及所选的列,例如选择了field2意味着您应该将其附加到JSON数据.

It will be helpful to you, In your data, you should mention which is column selected for example field2 is selected means you should append that to your JSON data.

在您的情况下,我添加了新的键来查找列"selected_radio":"field2"这样的列,您应该在前端进行相同的操作

In your case, I have added new key to find which is column "selected_radio":"field2" like this you should do the same on your front end

这是更改JSON数据的关键部分

and here is the angular part of change your JSON data

var data={
    "phoneno": [
    {
        "field1": "Mohamed",
        "field2": "123456789",
        "field3": "Sameer"
    },
    {
        "field1": "Ganesh",
        "field2": "987654321",
        "field3": "Pandiyan" 
     }
  ],
    "sender": "ifelse",
    "message": "test",
    "selected_radio":"field2"
};
var new_data=[];
_.each(data.phoneno,function(obj){
new_data.push({"phoneno":obj[data.selected_radio],"sender":data.sender,"message":data.message});
})
console.log(new_data);

<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>

这篇关于使用基于单选按钮值的javascript(NodeJS)将数据插入mysql(Sequelize)中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆