简体   繁体   English

Node.Js函数在promise返回中调用一次时运行两次

[英]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. 我正在尝试根据用户输入创建尽可能多的Google幻灯片。 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. 当我运行代码时,它停止了,因为createSlide()函数在一个调用上运行了两次,并创建了两个具有相同ID的幻灯片。 Poking around on the web, I've only found things about Node.Js input from a browser. 在网上闲逛,我只发现了有关来自浏览器的Node.Js输入的信息。 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: 第一次运行正常,它可以完成预期的工作,但是在创建第二张幻灯片(id:slide_1)时,它将制作幻灯片,将文本加倍,例如:

Hi
bye

Would create 将创建

Hi
bye
Hi
bye

Then stop due to the fact that there cannot be two Ids that are the same. 然后停止,因为不能有两个相同的ID。 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. 我做了更多的工作,发现在我的代码r5.on中运行了两次。 I tried deleting the array.push("\\n"). 我尝试删除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(). 注意:我将askForNew()重命名为askYOrNoCheck()。

Once again thanks for your help. 再次感谢您的帮助。

Needed to add r5.close(). 需要添加r5.close()。 It took multiple inputs from one input. 从一个输入中获取了多个输入。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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