1

hello i have try bellow code but my code not work properly

tags.forEach(function(value) {
    var where1 = {};
    var attr1 = ['id'];
    attr1 = ['id'];
    where1['name'] = value;
    tagData['name'] = value;
    tagModel.getTag(where1, attr1, function(code2,result2){
        if(result2.length!=0) {
            var quick_start_tagData={'quick_start_id' : result1['id'], 'tag_id' :result2[0]['id']}
            quick_start_tagModel.saveData(quick_start_tagData, function(code2,result2){
            });
            console.log(quick_start_tagData); 
         } else {
              tagModel.saveData(tagData, function(code2,result2) {});
           }
     });
});

problem is for loop iterate no.of data when i check value of that data into table if table have same value then get its id and insert and if not than add new record and get its id and insert into another table

but first select query execute all time and than insert query execute in loop

how to solve this issue

Query Execute like that way

Executing (default):

SELECT id FROM pxl_tag AS pxl_tag WHERE pxl_tag.name = 'a1';

SELECT id FROM pxl_tag AS pxl_tag WHERE pxl_tag.name = 'a2';

SELECT id FROM pxl_tag AS pxl_tag WHERE pxl_tag.name = 'a3';

INSERT INTO pxl_tag (id,name,created_at,updated_at) VALUES (DEFAULT,'a3','2018-05-04 04:35:32','2018-05-04 04:35:32');

INSERT INTO pxl_tag (id,name,created_at,updated_at) VALUES (DEFAULT,'a3','2018-05-04 04:35:32','2018-05-04 04:35:32');

INSERT INTO pxl_tag (id,name,created_at,updated_at) VALUES (DEFAULT,'a3','2018-05-04 04:35:32','2018-05-04 04:35:32');

but i want first select and insert step by step

Bhargav Chudasama
  • 6,928
  • 5
  • 21
  • 39

2 Answers2

0

You can use each method from async library. For example:

UPDATE

async.each(tags, function(value, cb) {
  var where1 = {};
  var attr1 = ['id'];
  attr1 = ['id'];
  where1['name'] = value;
  tagData['name'] = value;
  tagModel.getTag(where1, attr1, function(code2, result2) {
    if (result2.length != 0) {
      var quick_start_tagData = {
        'quick_start_id': result1['id'],
        'tag_id': result2[0]['id']
      }
      quick_start_tagModel.saveData(quick_start_tagData, function(code2, result2) {
        cb();
      });
      console.log(quick_start_tagData);
    } else {
      tagModel.saveData(tagData, function(code2, result2) {
        cb();
      });
    }
  });
}, function(err) {
  if (err) {
    console.log('An error ocurred');
  } else {
    console.log('Nothing happens');
  }
});
giankotarola
  • 765
  • 7
  • 13
0

If I understood you correctly, the problem is not that your INSERTs doesn't run right after the SELECTs. Your problem is that you are calling async functions (getTag() and saveData()) inside a sync function loop (forEach()), thus, by the time that your async functions resolve, they get the last value for tagData which is defined inside the sync loop. You should give it a different scope to get the correct tagData value.

Try this (untested):

tags.forEach(function(value) {
    var where1 = {};
    var attr1 = ['id'];
    attr1 = ['id'];
    where1['name'] = value;
    tagData['name'] = value;
    saveTag(tagModel, quick_start_tagModel, where1, attr1, result1, tagData);
});

//Create a function so that tagData will have a different scope for each call
function saveTag(tagModel, quickStartTagModel, where, attr, result1, tagData) {
    tagModel.getTag(where, attr, function(code,result){
        if(result.length!=0) {
            var quickStartTagData = {'quick_start_id' : result1['id'], 'tag_id' :result[0]['id']}
            quickStartTagModel.saveData(quickStartTagData, function(code, result){});
            console.log(quickStartTagData); 
        } else {
            tagModel.saveData(tagData, function(code,result) {});
        }
    });
}

if you strictly need to run the SELECT and INSERT sequentially (and not async), you should look at promises and async library. This might help: Resolve promises one after another (i.e. in sequence)?

Edudjr
  • 1,676
  • 18
  • 28