Console log Vs TextBox value

Hello, I know this is a very weird occurrence; I have an interaction that I want to have the results appended as a textbox value. The console log shows the results of the loop very well but only the first item can be passed into a textbox. Here is y code, can anyone find something that I am doing wrong;

      **let**  results  =  **await**  wixData . query ( "Cart" ) 

. eq ( “title” , sessionid )
. find ()
for ( var i = 0 ; i <= results . length ; i ++) {
if ( i === results . length ) break ;
let exam1 = await results . items [ i ]. course + " " + results . items [ i ]. board + " " + results . items [ i ]. brand + " " + results . items [ i ]. tier + " " + results . items [ i ]. amount

$w ( "#textBox1" ). value = exam1 

$w ( “#dataset3” ). setFieldValues ( {
“firstName” : $w ( ‘#input4’ ). value ,
“lastName” : $w ( ‘#input10’ ). value ,
“email” : $w ( ‘#input2’ ). value ,
“phone” : $w ( ‘#input11’ ). value ,
“exam” : exam1 ,
“amount” : $w ( ‘#input5’ ). value ,
“id” : $w ( ‘#uploadButton1’ ). value ,
} );
$w ( ‘#button3’ ). collapse ()
$w ( ‘#button2’ ). expand ()
console . log ( exam1 )
}}

For each iteration of the loop, you are setting #textBox1 to a new text value. What you want to do is to concatenate the strings created in each iteration of the loop, and then when the loop is finished set #textBox1 to this value. Something like this (not tested):

let textValue = '';
for (var i = 0; i <= results.length; i++) {
   if (i === results.length) break;
   let exam1 = await results.items[i].course + "               " + results.items[i].board + "         " + results.items[i].brand + "         " + results.items[i].tier + "         " + results.items[i].amount + '\n';
   console.log(exam1);
   textValue += exam1;
}
console.log(textValue);
$w("#textBox1").value = textValue;

Some observations and questions…

You don’t need await in the following statement - await is only needed for asynchronous tasks (such as database queries, calls to backend functions, etc).
You have this:

let exam1 = await results.items[i].course+"               "+results.items[i].board+"         "+results.items[i].brand+"         "+results.items[i].tier+"         "+results.items[i].amount

Delete await from the above statement.

There is no need for these lines to run for each iteration of the loop:

$w('#button3').collapse()
$w('#button2').expand()

What are you trying to do with $w(“#dataset3”).setFieldValues? You set the values but then you don’t do anything with the dataset after that.

The items are saved when a user has paid and submitted the form.

@paiysurv The $w ( #dataset3 ). setFieldValues ? API only applies to the current dataset item. Each time through your loop, new values overwrite the previous values that were set. When you eventually save the dataset item, only the last values will be saved.

Thank you Ysrael