gpt4 book ai didi

javascript - Node.Js 函数在 Promise 返回时调用一次时会运行两次

转载 作者:太空宇宙 更新时间:2023-11-04 01:40:06 25 4
gpt4 key购买 nike

我正在尝试根据用户输入创建所需数量的 Google Slide 幻灯片。理论上,如果我能解决这一问题,我的代码应该可以工作。问题是,我不能。当我运行代码时,它会停止,因为 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()。

再次感谢您的帮助。

最佳答案

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

关于javascript - Node.Js 函数在 Promise 返回时调用一次时会运行两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53220975/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com