How to refer to another Collections or "Accounts"

@jingyao97, @boxabrain, @burchaz

Please open a new thread if your problem is kind of related, but different. You can link to the related thread instead. Sometimes I’m bored and I scroll through the latest threads - but I don’t often click an threads, that have a solution already…

edit: @jingyao97 Sorry, of cause you authored the thread. Please ignore the sentence above.

But as always, there are multiple ways to achieve this goal.

  1. Do a second api request

https://example.com/api/cockpit/listUsers?token=xxtokenxx&filter[_id]=user_id

  1. Modify contents on an event in /config/bootstrap.php
<?php
// return account name instead of account _id when requesting
// collection via /api/collections/get/test2
// collection has a field named "account" with type "account-link"
if (COCKPIT_API_REQUEST) {

    $app->on('collections.find.after.test2', function($name, &$entries) {

        foreach ($entries as &$entry) {

            if (isset($entry['account'])) {
                // find account
                $account = $this->storage->findOne('cockpit/accounts', ['_id' => $entry['account']]);
                // return account name
                $entry['account'] = !empty($account['name']) ? $account['name'] : $account['user'];
                // return account array
                // $entry['account'] = $account; // caution: unset sensible data first!
            }

        }

    });

}
  1. Create a custom api endpoint

Use a similar logic like above to populate the account information.

Caution: Don’t return all account data directly or unset email, api_key, password and _reset_token before passing $entry['account'] = $account!