Contentful logo

Contentful Community

Getting linked items in a getEntries() call

Hey, I’m integrating with the restful API using Node and I have a contentModel (StrategyGroups) which can have child (Content - Strategy) items. The link is made in the Content - Strategy type to the parent StrategyGroups model. When I retrieve an item in the StrategyGroup, I also want all child Content - Strategy items. I’ve tried adding “include” on the client.GetEntries call, but they don’t come back. Any working examples of this I can refer to?

const contentful = require('contentful')
​
const client = contentful.createClient({
  space: process.env.space,
  accessToken: process.env.spaceapi,
  resolveLinks: true
})
​
​
exports.strategy_summary_get = function (req, res) {
​
  var slug = req.params.id;
  var strategyGroup;
 
  Promise.all([
    client.getEntries({
     'content_type': 'strategyGroup',
     'fields.slug': slug,
     include: 2
    })
   ])
   .then(([s]) => {
    strategyGroup = s,
   
    res.render('strategy/summary', {
     strategyGroup
    });
   })
   .catch(error => {
    console.log(error);
   });
 
 }

This returns:

{
   "sys":{
  "space":{
     "sys":{
        "type":"Link",
        "linkType":"Space",
        "id":"4e3rqs7p1vke"
     }
  },
  "id":"4qpzFGwbPipCE6Ek7UlyqG",
  "type":"Entry",
  "createdAt":"2019-08-07T08:11:56.473Z",
  "updatedAt":"2019-08-07T09:15:14.507Z",
  "environment":{
     "sys":{
        "id":"master",
        "type":"Link",
        "linkType":"Environment"
     }
  },
  "revision":4,
  "contentType":{
     "sys":{
        "type":"Link",
        "linkType":"ContentType",
        "id":"strategyGroup"
     }
  },
  "locale":"en-US"
   },
   "fields":{
  "title":"Prevention and education",
  "slug":"prevention-and-education",
  "description":"Towards a collective and clear prevention plan applying the right mix of interventions",
  "enabled":true,
  "order":1,
  "linkedContentGroup":{
     "sys":{
        "space":{
           "sys":{
              "type":"Link",
              "linkType":"Space",
              "id":"4e3rqs7p1vke"
           }
        },
        "id":"6kaeOzH3wzfjE8DKaaHuRP",
        "type":"Entry",
        "createdAt":"2019-08-07T08:03:55.673Z",
        "updatedAt":"2019-08-07T08:07:25.404Z",
        "environment":{
           "sys":{
              "id":"master",
              "type":"Link",
              "linkType":"Environment"
           }
        },
        "revision":2,
        "contentType":{
           "sys":{
              "type":"Link",
              "linkType":"ContentType",
              "id":"contentGroup"
           }
        },
        "locale":"en-US"
     },
     "fields":{
        "title":"Strategy",
        "slug":"strategy",
        "menuOrder":1,
        "enabled":true
     }
  }
   }
}