无法附加json数据

时间:2018-11-01 06:21:38

标签: mysql node.js json mongodb

正在尝试执行mongo查询和mysql查询,结果都将验证并返回到变量中。 我的问题是它仅返回mysql数据。不附加到mongo db结果。如果我放置console.log,那么它会显示,但不会返回。请让我知道以下代码中缺少的地方。

   var message= {'msg':gloabl_vars.messages.success,'mysql':{'msg':'success','data':[]},'mongo':{'msg':'success','data':[]}}; 
var MongoClient = require('mongodb').MongoClient;
      var url=gloabl_vars.mongodb_data.url;  
      MongoClient.connect(url,function(err, db) {
        if(err) throw err;
        var dbo=db.db("profilemanager");       
        dbo.collection('userinfo').find({_id:userdata.msisdn},{'_id':0,'subscriptions':1}).toArray(function(err,result){
          if(err) throw err;
          var dumpdata=result[0].subscriptions;         
          for(var i=0;i<dumpdata.length;i++){
            var dump=JSON.parse(JSON.stringify(dumpdata[i]));     
            console.log(dump.state); console.log(dump.reqChrgCode); 
            console.log(dump.cCode);           
            if(dump.state==1 && dump.reqChrgCode==userdata.code && dump.cCode==userdata.service_code){
              message.mongo.data['mongodb_userinfo']='Mongodb details updated.';
              message.mongo.msg="Success";
              message.msg="Success";
            }
            else{            
              message.mongo.msg="Failed";
              message.msg="Failed";
              message.mongo.data['mongodb_userinfo']='Failed to update data in mongodb.'; 
            }
          }     
        });
      });

    sql.query(db_queries, params, function(
          err,
          results
        ) {
          if (err) {
            console.log("error: ", err);
            result(err, null);
          } else {  
            var success_count=0;
            var running_count=0;
            for(let i=0;i<queries.length;i++){
              var temp=results[i]; 
              if(temp.length > 0){ 
                message.mysql.data[i] = {"info" :queries[i],"msg":"Success"};                    
                success_count++;
                running_count++;
              }
              else {
                message.mysql.data[i] = {"info" :queries[i],"msg":"Failed"};    
                success_count--;
                running_count++;
              }           
              if(success_count==(queries.length-1)) {
                message.mysql.msg="Success";
              } 
              else if(running_count==(queries.length-1)){
                message.msg="Failed";
                message.mysql.msg="Failed";          
              }           
            }
            result(null, message);
          }      
        });

预期输出:

{
    "message": {
        "msg": "Failed",
        "mysql": {
            "msg": "Failed",
            "data": [
                {
                    "info": "SELECT * FROM imidigitaltrans_logs.gw_api_trans_log where gtrid=?;",
                    "msg": "Failed"
                },
            ]
        },
        "mongo": {
            "msg": "success",
            "data": [
                "userinfo":"Updated"
            ]
        }
    }
}

实际:

{
    "message": {
        "msg": "Failed",
        "mysql": {
            "msg": "Failed",
            "data": [
                {
                    "info": "SELECT * FROM imidigitaltrans_logs.gw_api_trans_log where gtrid=?;",
                    "msg": "Failed"
                },
            ]
        },
        "mongo": {
            "msg": "success",
            "data": []
        }
    }
}

预先感谢

1 个答案:

答案 0 :(得分:0)

在数组中存储值的方式不正确。

更改

message.mongo.data['mongodb_userinfo']='Mongodb details updated.';

收件人

message.mongo.data[i] = {'mongodb_userinfo': 'Mongodb details updated.'};

您的ELSE情况也一样。