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

时间:2018-11-09 06:41:34

标签: javascript node.js google-slides-api

我正在尝试根据用户输入创建尽可能多的Google幻灯片。从理论上讲,如果可以解决此问题,我的代码应该可以工作。问题是,我不能。当我运行代码时,它停止了,因为createSlide()函数在一个调用上运行了两次,并创建了两个具有相同ID的幻灯片。在网上闲逛,我只发现了有关来自浏览器的Node.Js输入的信息。第一次运行正常,它会执行预期的操作,但是在创建第二张幻灯片(id:slide_1)时,它将制作幻灯片,将文本加倍,例如:

Hi
bye

将创建

Hi
bye
Hi
bye

然后停止,因为不能有两个相同的ID。这是我的代码(自创建幻灯片以来,我已经删除了请求):

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);
        }
    });
}

在此先感谢您的帮助!

编辑
我做了更多的工作,发现在我的代码r5.on中运行了两次。我尝试删除array.push(“ \ n”)。但这不能解决问题。数组输出为

[hi, bye, hi, bye]

这是我的新代码,再次删除了请求:

//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);
    });
}

注意: 我将askForNew()重命名为askYOrNoCheck()。

再次感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

需要添加r5.close()。从一个输入中提取了多个输入。