Question: So I have this async problem wherein I use async eachSeries to iterate, findById in mongoose, then call a function within the findById...

Problem is, I can't figure out how to get the function call to resolve before it is pushed into the array, and I end up with items\_results equaling an array of undefineds. 

I'm new to async and to promises, so this might be a mess.

```
    var user = req.user;
    var user_items = req.user.items;
    var item_results = [];
    var queue = Promise.resolve();
    var promises = [];

    var my_iterator = function(user_item_id, callback) {
      Item.findById(user_item_id, function(err, user_item) {
        queue.then(function() {
        return outside_function(user, user_item, null, false);
        });
        promises.push(queue);
        callback();
      });
    };

    var result = function() {
      var items_object = {
          type: 'success',
          items: item_results
      };
      res.status(200).send(items_object);
    };

    async.eachSeries(user_items, my_iterator, function(err) {
       Promise.all(promises).then(function (results) {
        item_results.push(results);
        result();
    });
});

```

Answer: 
The code below uses <https://github.com/lib/pq>

```
    import (
        "database/sql"
        "fmt"
        "github.com/lib/pq"
    )

func bulkCopyTest() {
    psqlInfo := fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=%s sslmode=verify-full",
        host, port, user, password, dbname)
    db, err := sql.Open("postgres", psqlInfo)
    if err != nil {
        panic(err)
    }
    defer db.Close()

    tx, err := db.Begin()

    if err != nil {
        panic(err)
    }

    stmt, err := tx.Prepare(pq.CopyInSchema("schemaName", "DBName", "columnName", "columnName"))
    if err != nil {
        panic(err)
    }

    //loop through an array of struct filled with data, or read from a file
    for _, row := range loadCsv {
        stmt.Exec(row.value1, row.value2)
        if err != nil {
            panic(err)
        }
    }

    _, err = stmt.Exec()
    if err != nil {
        panic(err)
    }
    err = stmt.Close()
    if err != nil {
        panic(err)
    }
    err = tx.Commit()
    if err != nil {
        panic(err)
    }
}

```
A rough template to execute COPY command can be found here - <https://play.golang.org/p/6y5v3IW8kD>

The code below should work for you.

```
   func copyTest() {

    psqlInfo := fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=%s sslmode=verify-full",host, port, user, password, dbname)
    db, err := sql.Open("postgres", psqlInfo)
    if err != nil {
        panic(err)
    }
    defer db.Close()

    err = db.Ping()
    if err != nil {
        panic(err)
    }

    tx, err := db.Begin()
    if err != nil {
        panic(err)
    }

    _, err = tx.Exec("copy metadata.charvalues_temp from 'C:\\yourlocation\\yourfile.csv' with csv")
    if err != nil {
        panic(err)
    }
    err = tx.Commit()
    if err != nil {
        panic(err)
    }
}

```