List of references has different order than what i see inside the contentful editor

Hello,

I have a problem when retrieving contents with the Javascript SDK

  • I have a “work” content type like this (see image #1)

  • And i have a “works” content type like this: (see image #2)

  • The “works” content is a list that references the “work” contents, this is what it looks like (see image #3) (sorry limit of 2 links)

The problem is that when getting the data the items are not ordered like what i see inside contentful, they seem random.

I whould expect “Andrea Colosio - Demo Reel” to be the first element in the array, instead i get “Dynamic Dialogue System – Wwise Project”

Is there a parameter that i have to send when retrieving the data or am i misunderstanding how the reference lists work?

Thanks in advance,
Loris

Hi @loris.bettazza,

As you rearrange your entries through the Web Interface, their specified order will be followed when you retrieve them directly through our APIs, but is not guaranteed unless you use the order param.

That being the case, could you send us a bit more information as to how you’ve configured this query in your JS application? (please omit any tokens, credentials or identifiers)

Hi @gabriel, thanks for the quick reply.

Here’s the code that i’m using, I pass an array of content types to a function that gets the entries of these content types.
The entries are then stored in a JSON file for later use.

const fs = require('fs');
const contentful = require('contentful');

const clientOptions = {
  space: 'SECRET',
  accessToken: 'SECRET',
};

const client = contentful.createClient(clientOptions);

function logError(error) {
  console.error(error);
}

async function getEntriesOfContentType(contentTypes) {
  const entriesByContentType = {};

  await Promise.all(contentTypes.map(contentType => (
    client.getEntries({
      content_type: contentType,
      locale: '*',
    })
      .then((entries) => {
        entriesByContentType[contentType] = entries.items;
      })
      .catch(logError)
  )))
    .then(response => response)
    .catch(logError);

  return entriesByContentType;
}

async function writeContentfulFile() {
  const contentTypes = ['works'];
  const entries = await getEntriesOfContentType(contentTypes);

  fs.writeFileSync('results.json', JSON.stringify(entries, null, 2));
}

writeContentfulFile();

Order of results when inspecting the results.json file:

  1. Dynamic Dialogue System – Wwise Project
  2. Car Chase – Car Engines SFX Edit
  3. City Soundscape – Wwise project
  4. Waves Test Package – Sound Design Project
  5. World of Warcraft – Music Edit
  6. Looking Around
  7. Random Music Machine – Max MSP Patch
  8. Andrea Colosio – Demo Reel
  9. Teto – Gameplay
  10. Teto – Breakdown
  11. Horizon Zero Dawn – VFS Final Sound Design Project
  12. TE Fire – Sound Design Edit
  13. Hostile Inn Episode 2 – Episodic Film Collaboration

Expected order:

  1. Andrea Colosio – Demo Reel
  2. Looking Around
  3. Horizon Zero Dawn – VFS Final Sound Design Project
  4. Hostile Inn Episode 2 – Episodic Film Collaboration
  5. Waves Test Package – Sound Design Project
  6. Teto – Breakdown
  7. Dynamic Dialogue System – Wwise Project
  8. Teto – Gameplay
  9. Car Chase – Car Engines SFX Edit
  10. City Soundscape – Wwise project
  11. TE Fire – Sound Design Edit
  12. Random Music Machine – Max MSP Patch
  13. World of Warcraft – Music Edit

Hi @loris.bettazza,
the order of the entries is not guaranteed unless you use the order param when querying the API.
You can order them by sys.createAt for example.

const fs = require('fs');
const contentful = require('contentful');

const clientOptions = {
  space: 'SECRET',
  accessToken: 'SECRET',
};

const client = contentful.createClient(clientOptions);

function logError(error) {
  console.error(error);
}

async function getEntriesOfContentType(contentTypes) {
  const entriesByContentType = {};

  await Promise.all(contentTypes.map(contentType => (
    client.getEntries({
      content_type: contentType,
      locale: '*',
      order: 'sys.createdAt'
    })
      .then((entries) => {
        entriesByContentType[contentType] = entries.items;
      })
      .catch(logError)
  )))
    .then(response => response)
    .catch(logError);

  return entriesByContentType;
}

async function writeContentfulFile() {
  const contentTypes = ['works'];
  const entries = await getEntriesOfContentType(contentTypes);

  fs.writeFileSync('results.json', JSON.stringify(entries, null, 2));
}

you learn more about the ordering here

1 Like

Thanks @khaled and @gabriel but i found the problem!

I think i messed up the naming when creating these content types inside contentful.
Basically i created the names backwards (works (plural) was a single work, and work (singular) was a list of works) so when i changed the names it DIDN’T change their IDs!

So now i have to remember that if i want the list i have to look at the property named “work”!

Sorry, and thanks again for the availability :slight_smile: