Trouble with my code (copy/paste Data between 2 collections)

Hallo Wix-Community,

i have some trouble with my code, which has a strange behaviour.
I have 2x Databases/Collections. The first one, called [Schwarzmarkt] has entries (items) made by different user and the second one, called [SM-Verwaltung], which has to collect just user-defined items (has to collect all the data of the user, which is currently logged in).

The code shown below, works, but not properly (not 100%)…

import wixData from 'wix-data';
import wixUsers from 'wix-users';

var BENUTZER
var BenutzerId
var Gefundeneitems
var selectedItem

$w.onReady(function () {
    BENUTZER = wixUsers.currentUser;
    BenutzerId = BENUTZER.id;
    console.log("BENUTZER = " + BENUTZER)
    console.log("Benutzer-ID = " + BenutzerId)
    $w("#input3").value = BenutzerId 

    füllung_DBsmverwaltung();
});

function füllung_DBsmverwaltung (parameter) {  
  wixData.truncate("SM-Verwaltung")

 let DatenQuery = wixData.query("Schwarzmarkt")
    .eq("_owner", BenutzerId)
    .find()
    .then((Results) =>  {
 let Gefundeneitems = Results.items;
 let Datenlänge = Results.length;
        console.log(Datenlänge)
 
 for (var i = 0; i < Gefundeneitems.length; i++) {
            console.log(i)
 let toInsert = {
 "title":            Gefundeneitems[i].title,
 /*
 .....here are some more definitions (columns)
 */
            }
            wixData.insert("SM-Verwaltung", toInsert).then( (results) => {let item = results;})   
        }   
    })
}

And here a summary of what happens when the code runs…

The problem is, that if there are more than 11 or 12 items in [Schwarzmarkt], after the code has done his job, not all items of a specific user have been found.

In this example 12-Items have been found of 17. The result had to be —> 17 !

Is there anybody, who could explain all that behaviour step by step.

I think it is like always a problem with ----> .then() / await / async-coding (asynchronous code)
which is really not of my strengths.

Thank for your HELP an EXPLANATION!


EDIT:

A promise-error like it seems to be, but whats the problem and how to solve?

Inserting during a for-loop is error prone, you send an insert 1 by 1 over the connection and some might get lost. The better way to do this is build an array with all data on the (17) items using array.push, then use wixData Bulkinsert to insert them all at once (so you only have 1 call instead of 17).

Hello Giri Zano,
thanks for reply. I never worked with that king of code before, i will give it a try.
I hope i can do that on my own.
Big thanks!

It´s not that difficult. Instead of doing 1

let toInsert = {
 "title":            Gefundeneitems[i].title,
 /*
 .....here are some more definitions (columns)
 */
            }

you declare an array (e.g. let arrToInsert = ;), set up a for-loop for all items to be inserted (you have it now already), do an arrToInsert.push(toInsert) for every item and then a
wixData.bulkInsert("“SM-Verwaltung”, arrToInsert) , with a .then etc, just like you did the single insert. Let me know if you get stuck, but you must first try(and learn), OK?

For array.push doc, look here: JavaScript Array push() Method
Doc on bulkInsert, you should be able to find yourself in Wix API docs.

Hallo Giri Zano,

THANK YOU very much for your help, but i have already found my solution about 40min ago (so it took almost 24-hours for me to find a solution which works) :roll_eyes::roll_eyes::roll_eyes::roll_eyes::sweat_smile::sweat_smile::sweat_smile::sweat_smile:

Yes for you its perhaps not so difficult, but i had to read about more than 8-10 posts and had made in that time about 25-tries (codings) to get what i want :rofl::rofl::rofl::rofl::rofl::rofl::rofl:

My biggest problems are such things like this, i try to show it somehow on the following example (step by step)…

  1. What i could do without any problems is to find in the Corvid-API the code for bulkInsert…
import wixData from 'wix-data';

let toInsert1 = {
  "title":        "Mr.",
  "first_name":   "John",
  "last_name":    "Doe"
};

let toInsert2 = {
  "title":        "Ms.",
  "first_name":   "Jane",
  "last_name":    "Doe"
};

wixData.bulkInsert("myCollection", [toInsert1, toInsert2])
  .then( (results) => {
    let inserted = results.inserted; // 2
    let insertedIds = results.insertedItemIds; // see below
    let updated = results.updated; // 0
    let skipped = results.skipped; // 0
    let errors = results.errors; // []
  } )
  .catch( (err) => {
    let errorMsg = err;
  } );

I tried multiple times to modify that API-exaple to my own needs and wanted to do something like that…

let toInsert[i] ={"title":"Mr.","first_name":"John","last_name":"Doe"};

…to create an amount of objects like —> toInsert1, toInsert2, toInsert3 and so on by a for-loop…

for (var i = 0; i < 10; i++){ .......... }

…so that i could do the same at this part of code…

wixData.bulkInsert("myCollection",[toInsert[i]])

…also to put it into a loop later, like this.

for (var i = 0; i < 10; i++){ 
    wixData.bulkInsert("myCollection",[toInsert[i]])
 }

But due to my lack of knowledge, I have not achieved my goal.

And at the end after almost 24-hours of search, i found this post here…

And here is my solution which works perfektly for me right now

function TEIL1 (parameter) {
 let toInsertArray =  [ ]
 let USER = wixUsers.currentUser;
 let UserID = wixUsers.currentUser.id;
    console.log("USER = " + USER);
    console.log("USER-ID = " + UserID);

    wixData.truncate("SM-Verwaltung");

 let DatenQuery = wixData.query("Schwarzmarkt");
    DatenQuery.eq("_owner", UserID);
    DatenQuery.find()

    .then((Results) =>  {console.log("Teil-II")
        Gefundeneitems = Results.items;
 let Datenlänge = Gefundeneitems.length;
 
 for (var i = 0; i < Datenlänge; i++){
 let toINSERT =  { 
 "title":            Gefundeneitems[i].title,
 "server":           Gefundeneitems[i].server, 
 "itemTyp":          Gefundeneitems[i].itemtyp,
            }
            toInsertArray.push(Object.assign ({ }, toINSERT )); 

 let opts = $w("#DDuseritems").options
                opts.push({"label": Gefundeneitems[i].title, "value": Gefundeneitems[i].title})
            $w("#DDuseritems").options = opts
        }
    })

    .then(()=>{
        wixData.bulkInsert ( "SM-Verwaltung", toInsertArray) 
        .then(( Ergebnisse ) =>  { 
        }) 
    })
}

EDIT: The result can be seen here …
https://mt2-king.wixsite.com/website/sm-benutzerkonto

(Just registrated user which already created some ITEMS, can see the results!)
Then it looks like this…

And every ITEM is now in the database, like it should be…

@ Giri Zano,

perhaps you can show your solution?
How you would solve the problem

if you had to use that code-snipet…???

import wixData from 'wix-data';

// ...

let toInsert1 = {
  "title":        "Mr.",
  "first_name":   "John",
  "last_name":    "Doe"
};

let toInsert2 = {
  "title":        "Ms.",
  "first_name":   "Jane",
  "last_name":    "Doe"
};

wixData.bulkInsert("myCollection", [toInsert1, toInsert2])
  .then( (results) => {
    let inserted = results.inserted; // 2
    let insertedIds = results.insertedItemIds; // see below
    let updated = results.updated; // 0
    let skipped = results.skipped; // 0
    let errors = results.errors; // []
  } )
  .catch( (err) => {
    let errorMsg = err;
  } );

The code that you found does exactly what I proposed: run a for-loop, build the toInsert, .push it onto an array and then bulkinsert it. You´ve got it.

Hello Giri Zano one more time.
Thanks!
But i have still one question.
Is there a way to write this code-line…

toInsertArray.push(Object.assign ({ }, toINSERT )); 

…because i do not really understand this codeline.

(Object.assign ({}, toINSERT )

How to explain that piece of code?

I would be happy to understand that to 100%

THX.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign

Thx @ ayleung66,

i will look at this.

J.D. and I had a bit of a discussion about this, if this was really necessary (the object.asign). Turns out it is not: you can simply declare an array, put objects into it and hand it over to the bulkInsert. Below a snippet of some of my code to illustrate:

 let arrUpdate = [];

 for (var i = 0; i < objEmailList.items.length; i++) {
 var toUpdate = {
 "_id": objEmailList.items[i]._id,
 "title": objEmailList.items[i].title,
 "fldDateEmailSent": objEmailList.items[i].fldDateEmailSent,
 "fldDateEmailUntil": objEmailList.items[i].fldDateEmailUntil,
 "fldUnsubscribe": true
                };
                arrUpdate.push(toUpdate);
               
 let objResult = await wixData.bulkUpdate("xEmails", arrUpdate, options).catch((err) => {
               

@giri-zano

THX, i will take few days to study all that stuff, which is new for me.
My code now works much better, but still not perfekt. There are still issues like:

  1. a limitation to 50-Items (shows just 50-founded items)
  2. does not show similar Items… i think because of this …
toInsertArray.push(Object.assign ({ }, toINSERT )); 

@russian-dima Ok, let me know if you need a hand

@giri-zano Hello Giri,

ok i think i have done the first correction in my code.
Indeed i could use and modify your suggested code and put it into my own one.

Now my code looks like …

function TEIL1 (parameter) {
 let toInsertArray =  [ ]
 let USER = wixUsers.currentUser;
 let UserID = wixUsers.currentUser.id;
    console.log("USER = " + USER);
    console.log("USER-ID = " + UserID);

    wixData.truncate("SM-Verwaltung");

 let DatenQuery = wixData.query("Schwarzmarkt");
    DatenQuery.eq("_owner", UserID);
    DatenQuery.find()
//  DatenQuery.limit(100)
    .then((Results) =>  {console.log("Teil-II")
        Gefundeneitems = Results.items;
 let Datenlänge = Gefundeneitems.length;
        $w('#TXTangebote').value= Datenlänge;
 
 for (var i = 0; i < Datenlänge; i++){
 let toINSERT =  { 
 "title":            Gefundeneitems[i].title,
 "server":           Gefundeneitems[i].server, 
 "reich":            Gefundeneitems[i].reich, 
 "rasse":            Gefundeneitems[i].rasse,
 "itemTyp":          Gefundeneitems[i].itemtyp,
 "itemLevel":        Gefundeneitems[i].level,
 "itemStufe":        Gefundeneitems[i].stufe,
 "itemPreis":        Gefundeneitems[i].preis,
 "wahrung":          Gefundeneitems[i].waehrung,
 "menge":            Gefundeneitems[i].menge,
 "bildUrl":          Gefundeneitems[i].bildUrl,
 "verkaufer":        Gefundeneitems[i].verkaufer,
 "nachricht":        Gefundeneitems[i].nachricht,
 "ursprungsId":      Gefundeneitems[i]._id,
 "bonus1":           Gefundeneitems[i].bonus1,
 "bonus2":           Gefundeneitems[i].bonus2,
 "bonus3":           Gefundeneitems[i].bonus3,
 "bonus4":           Gefundeneitems[i].bonus4,
 "bonus5":           Gefundeneitems[i].bonus5,
 "bonus6":           Gefundeneitems[i].bonus6,
 "bonus7":           Gefundeneitems[i].bonus7,
 "p1":               Gefundeneitems[i].p1,
 "p2":               Gefundeneitems[i].p2,
 "p3":               Gefundeneitems[i].p3,
 "p4":               Gefundeneitems[i].p4,
 "p5":               Gefundeneitems[i].p5,
 "p6":               Gefundeneitems[i].p6,
 "p7":               Gefundeneitems[i].p7,
 "geiststein1":      Gefundeneitems[i].geiststein1,
 "geiststein2":      Gefundeneitems[i].geiststein2,
 "geiststein3":      Gefundeneitems[i].geiststein3,              
 "element":          Gefundeneitems[i].element,
 "elementStufe":     Gefundeneitems[i].elementStufe,
 "elementStarke":    Gefundeneitems[i].elementstarke,
 "effektivitat":     Gefundeneitems[i].elementEffektivitat
            }
 //  toInsertArray.push(Object.assign ({ }, toINSERT )); 
            toInsertArray.push(toINSERT);


 let opts = $w("#DDuseritems").options
                opts.push({"label": Gefundeneitems[i].title, "value": Gefundeneitems[i].title})
            $w("#DDuseritems").options = opts
        }
    })
    
//  .then(()=>{
//      wixData.bulkInsert ("SM-Verwaltung", toInsertArray) 
//          .then(( Ergebnisse ) =>  { 
//      }) 
//  })
    .then(()=>{
        wixData.bulkInsert("SM-Verwaltung", toInsertArray)
    })
}

I did not use the async + await because i’M still not good with that kind of code.
Instead of await i took —> .then(()=>{ })-command.

I#m really glad that now i can use an array-command instead of that kind of code-snipet here ---->(Object.assign ({ }, toINSERT ), because i am more familiar with Arrays like ----> toInsertArray = [ ] / toInsertArray.push(toINSERT)

Overall, with your help (Geri & ayleung66) i could improve a little bit my skills in wix-coding, thank you for that!

But i have still 2-issues.

Starting-Point:
I have a database (“database1”) which has a column/reference named for example (“items”) which is filled up with 60-items.

  1. After the above code has done his work i have a choice of just 50-items (10-items less) then at the beginning.
    A limitation-problem?

  2. And i can not choose similar entries. For example if there are 5-similar entries of an item calles “xyz”, after choosing of one of them, the result is always the same, although all items are different, just the name of item (“title”) is the same.

Someone any idea, what is the problem in my code?

@russian-dima ad 1: well, you do truncate (=kill) the collection at the beginning
ad 2) do not understand