简体   繁体   中英

Node.Js function runs twice when called once on promise return

I am attempting to create as many Google Slide slides as needed based on user input. In theory, my code should work if I can fix this one problem. The problem is, I can't. When I run my code, it stops because the createSlide() function runs twice on one call and creates two slides with the same Id. Poking around on the web, I've only found things about Node.Js input from a browser. First time through it runs ok, it does what it's supposed to, but when slide two (id: slide_1) is created it makes the slide, doubles the text such as:

Hi
bye

Would create

Hi
bye
Hi
bye

Then stop due to the fact that there cannot be two Ids that are the same. Here is my code (I've deleted the requests since the slide creation works fine):

function askYOrN(auth){

    console.log("HELLO");

    const r4 = readline.createInterface({
        input: process.stdin,
        output: process.stdout
    });

    r4.question("Do you want to add a slide [Y/N]? ", function(yesOrNo){
        yesOrNoLog = yesOrNo;
        r4.close();
        fAskForNewCounter = fAskForNewCounter + 1;
        askForNew(auth);
    });
}

//ASKS IF ANOTHER SLIDE IS NEEDED
function askForNew(auth){

    //READLINE
    const r5 = readline.createInterface({
        input: process.stdin,
        output: process.stdout, 
        terminal: false
    });

    //CLEARS txt
    txtArray = [];

    //CHECKS THE ANSWER AND DOES ACCORDINGLY
    if (yesOrNoLog == "yes" || yesOrNoLog == "y"){

        //ASKS FOR TEXT AND ASSIGNS IT TO THE VARIABLE
        r5.prompt();
        console.log("Text:\n");
        r5.on('line', function (textIn) {
            if (textIn == ".exit" || textIn == ".e" || textIn == ".next" || textIn == ".n"){
                createSlide(auth);
            }else{
                textArray.push(textIn);
                textArray.push('\n');
            }
        }); 
    }else if (yesOrNoLog == "no" || yesOrNoLog == "n"){

        //CREATES TITLE SLIDE
        createTitleSlide(auth);
    }else{

        //ASKS FOR VALID ANSWER
        if (fAskForNewCounter >= 0){
            console.log("Enter a valid answer");
            askYOrN(auth);
        }
    }
}

//DECLARATION FOR THE NUMBER COUNTER [ num ]
var num = 0;

function createSlide(auth) {

    //AUTHENTICATION
    const slides = google.slides({version: 'v1', auth});

    //CHANGES txtArray TO STRING
    var txt = txtArray.join('');

    //CHANGING VARS
    var slideId = 'slide_' + num;
    var pageId = slideId;

    var textId = 'text_box_' + num;
    var elementId = textId;

    var iIndex = num;

    //SLIDE NUMBER
    var sNum = num + 1;


        console.log(pageId);

    //ALL REQUESTS GO IN requests
         var requests = [{   
        }];

    //BATCH UPDATE
    return slides.presentations.batchUpdate({
        presentationId,
        resource: {
            requests,
        },
    }, (err, res) => {
        if (err) {
            error(err);
        }else{
            console.log("Slide #" + sNum + " has been created");

            //INCREASES COUNTER BY 1
            num = num + 1;

            //ASKS IF A NEW SLIDE WANTS TO BE CREATED
            var delay = 30;
            var lastClick = 0;

            if (lastClick >= (Date.now() - delay))
            return;
            lastClick = Date.now();

            yesOrNoLog = "";
            askYOrNo(auth);
        }
    });
}

Thanks in advance for your help!

EDIT
I have done some more work and have found that in my code r5.on is run twice. I tried deleting the array.push("\\n"). but this does not fix the problem. The array outputs as

[hi, bye, hi, bye]

This is my new code once again I have deleted the requests:

//ASKS IF YOU WANT A NEW SLIDE
function askYOrN(auth){

    console.log("askYOrN");

    const r4 = readline.createInterface({
        input: process.stdin,
        output: process.stdout,
        terminal: false
    });

    r4.question("Do you want to add a slide [Y/N]? ", function(yesOrNo){
        console.log("aksYOrN question");
        yesOrNoLog = yesOrNo;
        r4.close();
        yOrNCheckCounter = yOrNCheckCounter + 1;
        askYOrNCheck(auth);
    });
}

//ASKS IF ANOTHER SLIDE IS NEEDED
function askYOrNCheck(auth){
    console.log("askYOrNCheck begins");
    //READLINE
    const r5 = readline.createInterface({
        input: process.stdin,
        output: process.stdout, 
        terminal: false
    });

    //CLEARS TXT
    txtArray = [];

    //CHECKS THE ANSWER AND DOES ACCORDINGLY
    if (yesOrNoLog == "yes" || yesOrNoLog == "y"){

        //ASKS FOR TXT AND ASSIGNS IT TO THE VARIABLE
        console.log("prompt opens");
        r5.prompt();
        console.log("Text:");
        r5.on('line', function (textIn) {
            console.log("r5.on");
            if (textIn == ".exit" || textIn == ".e" || textIn == ".next" || textIn == ".n"){
                console.log("if next");
                createSlide(auth);
            }else{
                console.log(txtArray);
                console.log("about to push text");
                txtArray.push(textIn);
                console.log(txtArray);
            }
        }); 
    }else if (yesOrNoLog == "no" || yesOrNoLog == "n"){

        //CREATES TITLE SLIDE
        createTitleSlide(auth);
    }else{

        //ASKS FOR VALID ANSWER
        if (yOrNCheckCounter >= 0){
            console.log("Enter a valid answer");
            askYOrN(auth);
        }
    }
}

//DECLARATION FOR THE NUMBER COUNTER [ num ]
var num = 0;

function createSlide(auth) {

    //AUTHENTICATION
    const slides = google.slides({version: 'v1', auth});

    //CHANGES txtArray TO STRING
    var text = txtArray.join('\n');

    //CHANGING VARS
    var slideId = 'slide_' + num;
    var pageId = slideId;

    var textId = 'text_box_' + num;
    var elementId = textId;

    var iIndex = num;

    //SLIDE NUMBER
    var sNum = num + 1;

    console.log(pageId);

    //ALL REQUESTS GO IN [ requests ]
            var requests = [];

    //BATCH UPDATE
    return slides.presentations.batchUpdate({
        presentationId,
        resource: {
            requests,
        },
    }, (err, res) => {
        if (err) return error(err);
        console.log("Slide #" + sNum + " has been created");

        //INCREASES COUNTER BY 1
        num = num + 1;

        //ASKS IF A NEW SLIDE WANTS TO BE CREATED
        askYOrN(auth);
    });
}

Note: I renamed askForNew() to askYOrNoCheck().

Once again thanks for your help.

Needed to add r5.close(). It took multiple inputs from one input.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM