javascript中的错误处理并以特定格式发送响应?

tez616oj  于 2021-07-29  发布在  Java
关注(0)|答案(0)|浏览(130)

我是javascript新手,我构建了一个api,下面是一段代码:

var dumpData = function(dataObject, callback)
{
    var unique_device_key = dataObject['unique_device_key'];
    var ip_address = dataObject['ip_address'];
    var data_array = dataObject['data_array']

    var sQuery = "Select count(1) as device_matching from sensiblyai.tb_devices_deployed where 
    unique_device_id = ?";

    executeQuery(sQuery, unique_device_key, function(err, data)
    {  
        if( data[0]['device_matching'] === 0 )
        {
            //It means, no such device is present in the database.
            var error_message = "You are not allowed to perform this operation";
            var return_object = {"code":1, "message":error_message};
            return callback(null, return_object);
        }

        var insertTasks = [];
        var insertQuery = "Insert into sensiblyai.tb_device_data_logs (unique_device_id, ip_address, 
        recorded_timestamp,sd_violations,mask_violations) values (?,?,?,?,?)ON DUPLICATE KEY UPDATE 
        ip_address = ?, recorded_timestamp = ?,sd_violations = ?,mask_violations = ?  ";

        for(var i= 0; i< data_array.length; i++)
        {
            var recorded_timestamp = moment_utc(data_array[i]['recorded_timestamp']).format("YYYY-MM- 
            DD HH:mm:ss");
            var sd_violations = data_array[i]['sd_violations'];
            var mask_violations = data_array[i]['mask_violations'];

            insertTasks.push(executeQuery.bind(null, insertQuery, [unique_device_key, ip_address, 
            recorded_timestamp,sd_violations,mask_violations, ip_address, 
            recorded_timestamp,sd_violations,mask_violations]));
        }
        async.series(insertTasks, function (err, data) {
            if(err)
            {
                console.log("Error in inserting data to database", err);
                return callback(err);
            }

            var response_object_array = [];
            for(var i = 0 ; i < data_array.length; i++)
            {
                var response_object = {};
                response_object['recorded_timestamp'] = data_array[i]['recorded_timestamp'];
                response_object['management_flag'] = 0;
                response_object['status'] = 1;
                response_object_array.push(response_object);
            }
            var return_object = {"code":2, "data":response_object_array};
            callback(null, return_object)
        })
    })
}

它接收json格式的输入,如下所示

{ "device_id": "test1",
  "array":[{"timestamp":"2020-07-20 22:37:00","count" :"9", "alerts":"0" },{"timestamp":"2020-07-20 
   22:48:00","sd_violations" :"6", "alerts":"3" }]
  }

并以

[
    {
        "recorded_timestamp": "2020-07-20 22:37:00",
        "management_flag": 0,
        "status": 1
    },
    {
        "recorded_timestamp": "2020-07-20 22:48:00",
        "management_flag": 0,
        "status": 1
    }
]

如果出错,则返回错误消息
现在我想要的是,api在json的同一数组中返回响应,它应该包含3个状态:
0:前端错误-1:后端错误1:成功
e、 g:如果我的代码试图插入10行,但插入3行时出现错误,那么api将返回10行,状态为0 1-1,对应行的数据。它不会返回任何错误消息。
如何在上面的代码中实现这一点?

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题