Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to set a custom id for a new entry? #929

Closed
srajiang opened this issue Sep 2, 2021 · 3 comments
Closed

How to set a custom id for a new entry? #929

srajiang opened this issue Sep 2, 2021 · 3 comments

Comments

@srajiang
Copy link

srajiang commented Sep 2, 2021

👋 I'm using the plain client, and getting the following error:

Invalid value "undefined" for header "X-Contentful-Content-Type"

when attempting to use entry.createWithId. I think I'm probably just trying to use this method the wrong way, but I can't seem to the find the equivalent of this for the plain client. Here's my code:

// set up plain scoped client 
const client = contentful.createClient({
  accessToken: process.env.CONTENTFUL_API_KEY
},
{
  type: 'plain',
  defaults: {
    spaceId: process.env.CONTENTFUL_SPACE_ID,
    environmentId: process.env.CONTENTFUL_ENV_ID,
  },
});

let res = await client.entry.createWithId('page', refId, { 
            fields: {
              source: `https://github.com/${process.env.REPOSITORY}/blob/main/${path}`,
              locale: frontMatter['lang'],
              markdown: fContent,
            }
          })

Would love to be pointed in the right direction of documentation for the plain client as well if possible.

@srajiang
Copy link
Author

srajiang commented Sep 2, 2021

Here's what I get when I call

https://api.contentful.com/spaces/{{space_id}}/environments/{{environment_id}}/public/content_types

{
    "sys": {
        "type": "Array"
    },
    "total": 4,
    "skip": 0,
    "limit": 100,
    "items": [
        {
            "sys": {
                "space": {
                    "sys": {
                        "type": "Link",
                        "linkType": "Space",
                        "id": **OMITTED**
                    }
                },
                "id": "page",   <-------- I am passing this currently into content_type_id as you can see in the request above, but it seems the X-Contentful-Content-Type is not being set
                "type": "ContentType",
                "createdAt": "2021-06-14T17:59:47.710Z",
                "updatedAt": "2021-09-02T17:07:19.834Z",
                "environment": {
                    "sys": {
                        "id": "master",
                        "type": "Link",
                        "linkType": "Environment"
                    }
                },
                "revision": 11
            },
            "displayField": "source",
            "name": "Page",
            "description": "One to many page sections",
            "fields": [
                {
                    "id": "source",
                    "name": "Source",
                    "type": "Symbol",
                    "localized": false,
                    "required": false,
                    "validations": [],
                    "disabled": false,
                    "omitted": false
                },
                {
                    "id": "primarySection",
                    "name": "Primary section",
                    "type": "Link",
                    "localized": true,
                    "required": false,
                    "validations": [],
                    "disabled": false,
                    "omitted": false,
                    "linkType": "Entry"
                },
                {
                    "id": "secondarySections",
                    "name": "Secondary sections",
                    "type": "Array",
                    "localized": true,
                    "required": false,
                    "validations": [],
                    "disabled": false,
                    "omitted": false,
                    "items": {
                        "type": "Link",
                        "validations": [],
                        "linkType": "Entry"
                    }
                },
                {
                    "id": "markdown",
                    "name": "Markdown",
                    "type": "RichText",
                    "localized": true,
                    "required": false,
                    "validations": [
                        {
                            "nodes": {}
                        }
                    ],
                    "disabled": false,
                    "omitted": false
                },
                {
                    "id": "author",
                    "name": "Author",
                    "type": "Array",
                    "localized": false,
                    "required": false,
                    "validations": [],
                    "disabled": false,
                    "omitted": false,
                    "items": {
                        "type": "Symbol",
                        "validations": []
                    }
                },
                {
                    "id": "locale",
                    "name": "Locale",
                    "type": "Symbol",
                    "localized": false,
                    "required": false,
                    "validations": [],
                    "disabled": false,
                    "omitted": false
                }
            ]
        },
        {
            "sys": {
                "space": {
                    "sys": {
                        "type": "Link",
                        "linkType": "Space",
                        "id": **OMITTED**
                    }
                },
                "id": "pageSection",
                "type": "ContentType",
                "createdAt": "2021-06-14T17:57:25.686Z",
                "updatedAt": "2021-06-17T20:43:19.851Z",
                "environment": {
                    "sys": {
                        "id": "master",
                        "type": "Link",
                        "linkType": "Environment"
                    }
                },
                "revision": 2
            },
            "displayField": "title",
            "name": "Page Section",
            "description": "Single section of documentation, with optional code snippets",
            "fields": [
                {
                    "id": "author",
                    "name": "Author",
                    "type": "Symbol",
                    "localized": false,
                    "required": false,
                    "validations": [],
                    "disabled": false,
                    "omitted": false
                },
                {
                    "id": "content",
                    "name": "Content",
                    "type": "RichText",
                    "localized": true,
                    "required": false,
                    "validations": [
                        {
                            "nodes": {}
                        }
                    ],
                    "disabled": false,
                    "omitted": false
                },
                {
                    "id": "snippets",
                    "name": "Snippets",
                    "type": "Array",
                    "localized": true,
                    "required": false,
                    "validations": [],
                    "disabled": false,
                    "omitted": false,
                    "items": {
                        "type": "Link",
                        "validations": [],
                        "linkType": "Entry"
                    }
                },
                {
                    "id": "title",
                    "name": "Title",
                    "type": "Symbol",
                    "localized": true,
                    "required": false,
                    "validations": [],
                    "disabled": false,
                    "omitted": false
                }
            ]
        },
        {
            "sys": {
                "space": {
                    "sys": {
                        "type": "Link",
                        "linkType": "Space",
                        "id":{
    "sys": {
        "type": "Array"
    },
    "total": 4,
    "skip": 0,
    "limit": 100,
    "items": [
        {
            "sys": {
                "space": {
                    "sys": {
                        "type": "Link",
                        "linkType": "Space",
                        "id": **OMITTED**
                    }
                },
                "id": "page",
                "type": "ContentType",
                "createdAt": "2021-06-14T17:59:47.710Z",
                "updatedAt": "2021-09-02T17:07:19.834Z",
                "environment": {
                    "sys": {
                        "id": "master",
                        "type": "Link",
                        "linkType": "Environment"
                    }
                },
                "revision": 11
            },
            "displayField": "source",
            "name": "Page",
            "description": "One to many page sections",
            "fields": [
                {
                    "id": "source",
                    "name": "Source",
                    "type": "Symbol",
                    "localized": false,
                    "required": false,
                    "validations": [],
                    "disabled": false,
                    "omitted": false
                },
                {
                    "id": "primarySection",
                    "name": "Primary section",
                    "type": "Link",
                    "localized": true,
                    "required": false,
                    "validations": [],
                    "disabled": false,
                    "omitted": false,
                    "linkType": "Entry"
                },
                {
                    "id": "secondarySections",
                    "name": "Secondary sections",
                    "type": "Array",
                    "localized": true,
                    "required": false,
                    "validations": [],
                    "disabled": false,
                    "omitted": false,
                    "items": {
                        "type": "Link",
                        "validations": [],
                        "linkType": "Entry"
                    }
                },
                {
                    "id": "markdown",
                    "name": "Markdown",
                    "type": "RichText",
                    "localized": true,
                    "required": false,
                    "validations": [
                        {
                            "nodes": {}
                        }
                    ],
                    "disabled": false,
                    "omitted": false
                },
                {
                    "id": "author",
                    "name": "Author",
                    "type": "Array",
                    "localized": false,
                    "required": false,
                    "validations": [],
                    "disabled": false,
                    "omitted": false,
                    "items": {
                        "type": "Symbol",
                        "validations": []
                    }
                },
                {
                    "id": "locale",
                    "name": "Locale",
                    "type": "Symbol",
                    "localized": false,
                    "required": false,
                    "validations": [],
                    "disabled": false,
                    "omitted": false
                }
            ]
        },
        {
            "sys": {
                "space": {
                    "sys": {
                        "type": "Link",
                        "linkType": "Space",
                        "id": **OMITTED**
                    }
                },
                "id": "pageSection",
                "type": "ContentType",
                "createdAt": "2021-06-14T17:57:25.686Z",
                "updatedAt": "2021-06-17T20:43:19.851Z",
                "environment": {
                    "sys": {
                        "id": "master",
                        "type": "Link",
                        "linkType": "Environment"
                    }
                },
                "revision": 2
            },
            "displayField": "title",
            "name": "Page Section",
            "description": "Single section of documentation, with optional code snippets",
            "fields": [
                {
                    "id": "author",
                    "name": "Author",
                    "type": "Symbol",
                    "localized": false,
                    "required": false,
                    "validations": [],
                    "disabled": false,
                    "omitted": false
                },
                {
                    "id": "content",
                    "name": "Content",
                    "type": "RichText",
                    "localized": true,
                    "required": false,
                    "validations": [
                        {
                            "nodes": {}
                        }
                    ],
                    "disabled": false,
                    "omitted": false
                },
                {
                    "id": "snippets",
                    "name": "Snippets",
                    "type": "Array",
                    "localized": true,
                    "required": false,
                    "validations": [],
                    "disabled": false,
                    "omitted": false,
                    "items": {
                        "type": "Link",
                        "validations": [],
                        "linkType": "Entry"
                    }
                },
                {
                    "id": "title",
                    "name": "Title",
                    "type": "Symbol",
                    "localized": true,
                    "required": false,
                    "validations": [],
                    "disabled": false,
                    "omitted": false
                }
            ]
        },
        {
            "sys": {
                "space": {
                    "sys": {
                        "type": "Link",
                        "linkType": "Space",
                        "id": "lfws4sw3zx32"
                    }
                },
                "id": "codeSample",
                "type": "ContentType",
                "createdAt": "2021-06-14T17:53:51.226Z",
                "updatedAt": "2021-06-14T17:53:51.226Z",
                "environment": {
                    "sys": {
                        "id": "master",
                        "type": "Link",
                        "linkType": "Environment"
                    }
                },
                "revision": 1
            },
            "displayField": "title",
            "name": "Code sample",
            "description": "Collection of code snippets",
            "fields": [
                {
                    "id": "title",
                    "name": "Title",
                    "type": "Symbol",
                    "localized": true,
                    "required": false,
                    "validations": [],
                    "disabled": false,
                    "omitted": false
                },
                {
                    "id": "description",
                    "name": "Description",
                    "type": "Text",
                    "localized": true,
                    "required": false,
                    "validations": [],
                    "disabled": false,
                    "omitted": false
                },
                {
                    "id": "tags",
                    "name": "Tags",
                    "type": "Array",
                    "localized": false,
                    "required": false,
                    "validations": [],
                    "disabled": false,
                    "omitted": false,
                    "items": {
                        "type": "Symbol",
                        "validations": []
                    }
                },
                {
                    "id": "source",
                    "name": "Source",
                    "type": "Symbol",
                    "localized": false,
                    "required": false,
                    "validations": [],
                    "disabled": false,
                    "omitted": false
                },
                {
                    "id": "snippets",
                    "name": "Snippets",
                    "type": "Array",
                    "localized": true,
                    "required": false,
                    "validations": [],
                    "disabled": false,
                    "omitted": false,
                    "items": {
                        "type": "Link",
                        "validations": [],
                        "linkType": "Entry"
                    }
                }
            ]
        },
        {
            "sys": {
                "space": {
                    "sys": {
                        "type": "Link",
                        "linkType": "Space",
                        "id": "lfws4sw3zx32"
                    }
                },
                "id": "codeSnippet",
                "type": "ContentType",
                "createdAt": "2021-06-14T17:31:14.604Z",
                "updatedAt": "2021-06-14T17:31:14.604Z",
                "environment": {
                    "sys": {
                        "id": "master",
                        "type": "Link",
                        "linkType": "Environment"
                    }
                },
                "revision": 1
            },
            "displayField": "author",
            "name": "Code Snippet",
            "description": "",
            "fields": [
                {
                    "id": "id",
                    "name": "ID",
                    "type": "Integer",
                    "localized": false,
                    "required": false,
                    "validations": [],
                    "disabled": false,
                    "omitted": false
                },
                {
                    "id": "author",
                    "name": "Author",
                    "type": "Symbol",
                    "localized": false,
                    "required": false,
                    "validations": [],
                    "disabled": false,
                    "omitted": false
                },
                {
                    "id": "language",
                    "name": "Language",
                    "type": "Symbol",
                    "localized": false,
                    "required": false,
                    "validations": [],
                    "disabled": false,
                    "omitted": false
                },
                {
                    "id": "content",
                    "name": "Content",
                    "type": "RichText",
                    "localized": true,
                    "required": false,
                    "validations": [
                        {
                            "nodes": {}
                        }
                    ],
                    "disabled": false,
                    "omitted": false
                },
                {
                    "id": "libraries",
                    "name": "Libraries",
                    "type": "Array",
                    "localized": false,
                    "required": false,
                    "validations": [],
                    "disabled": false,
                    "omitted": false,
                    "items": {
                        "type": "Symbol",
                        "validations": []
                    }
                }
            ]
        }
    ]
}
                    }
                },
                "id": "codeSample",
                "type": "ContentType",
                "createdAt": "2021-06-14T17:53:51.226Z",
                "updatedAt": "2021-06-14T17:53:51.226Z",
                "environment": {
                    "sys": {
                        "id": "master",
                        "type": "Link",
                        "linkType": "Environment"
                    }
                },
                "revision": 1
            },
            "displayField": "title",
            "name": "Code sample",
            "description": "Collection of code snippets",
            "fields": [
                {
                    "id": "title",
                    "name": "Title",
                    "type": "Symbol",
                    "localized": true,
                    "required": false,
                    "validations": [],
                    "disabled": false,
                    "omitted": false
                },
                {
                    "id": "description",
                    "name": "Description",
                    "type": "Text",
                    "localized": true,
                    "required": false,
                    "validations": [],
                    "disabled": false,
                    "omitted": false
                },
                {
                    "id": "tags",
                    "name": "Tags",
                    "type": "Array",
                    "localized": false,
                    "required": false,
                    "validations": [],
                    "disabled": false,
                    "omitted": false,
                    "items": {
                        "type": "Symbol",
                        "validations": []
                    }
                },
                {
                    "id": "source",
                    "name": "Source",
                    "type": "Symbol",
                    "localized": false,
                    "required": false,
                    "validations": [],
                    "disabled": false,
                    "omitted": false
                },
                {
                    "id": "snippets",
                    "name": "Snippets",
                    "type": "Array",
                    "localized": true,
                    "required": false,
                    "validations": [],
                    "disabled": false,
                    "omitted": false,
                    "items": {
                        "type": "Link",
                        "validations": [],
                        "linkType": "Entry"
                    }
                }
            ]
        },
        {
            "sys": {
                "space": {
                    "sys": {
                        "type": "Link",
                        "linkType": "Space",
                        "id": **OMITTED**
                    }
                },
                "id": "codeSnippet",
                "type": "ContentType",
                "createdAt": "2021-06-14T17:31:14.604Z",
                "updatedAt": "2021-06-14T17:31:14.604Z",
                "environment": {
                    "sys": {
                        "id": "master",
                        "type": "Link",
                        "linkType": "Environment"
                    }
                },
                "revision": 1
            },
            "displayField": "author",
            "name": "Code Snippet",
            "description": "",
            "fields": [
                {
                    "id": "id",
                    "name": "ID",
                    "type": "Integer",
                    "localized": false,
                    "required": false,
                    "validations": [],
                    "disabled": false,
                    "omitted": false
                },
                {
                    "id": "author",
                    "name": "Author",
                    "type": "Symbol",
                    "localized": false,
                    "required": false,
                    "validations": [],
                    "disabled": false,
                    "omitted": false
                },
                {
                    "id": "language",
                    "name": "Language",
                    "type": "Symbol",
                    "localized": false,
                    "required": false,
                    "validations": [],
                    "disabled": false,
                    "omitted": false
                },
                {
                    "id": "content",
                    "name": "Content",
                    "type": "RichText",
                    "localized": true,
                    "required": false,
                    "validations": [
                        {
                            "nodes": {}
                        }
                    ],
                    "disabled": false,
                    "omitted": false
                },
                {
                    "id": "libraries",
                    "name": "Libraries",
                    "type": "Array",
                    "localized": false,
                    "required": false,
                    "validations": [],
                    "disabled": false,
                    "omitted": false,
                    "items": {
                        "type": "Symbol",
                        "validations": []
                    }
                }
            ]
        }
    ]
}

@srajiang
Copy link
Author

srajiang commented Sep 2, 2021

I switched to using the regular client, and now it appears the X-Contentful-Content-Type header is correctly being set. However, when trying to use environment.createEntryWithId I am getting a 404.

I'm trying to create brand new entry and specify the ID. Am I using this incorrectly? This is my error message:

NotFound: {
  "status": 404,
  "statusText": "Not Found",
  "message": "The resource could not be found.",
  "details": {},
  "request": {
    "url": "/spaces/OMITTED/environments/master/entries/OMITTITED/OMITTED/docs/temp2.md",
    "headers": {
      "Accept": "application/json, text/plain, */*",
      "Content-Type": "application/vnd.contentful.management.v1+json",
      "X-Contentful-User-Agent": "sdk contentful-management.js/7.36.1; platform node.js/v14.17.6; os macOS/19.6.0;",
      "Authorization": "***",
      "user-agent": "node.js/v14.17.6",
      "Accept-Encoding": "gzip",
      "X-Contentful-Content-Type": "page",
      "Content-Length": 1391
    },
    "method": "put",
    "payloadData": "{\"fields\":{\"source\":{\"en-US\":\"https://github.com/slackapi/bolt-js/blob/main/docs/temp2.md\"},\"markdown\":{\"en-US\":\"---\\ntitle: Listening to messages\\nlang: en\\nslug: message-listening\\norder: 1\\n---\\n\\n<div class=\\\"section-content\\\">\\nTo listen to messages that [your app has access to receive](https://api.slack.com/messaging/retrieving#permissions), you can use the `message()` method which filters out events that aren’t of type `message`.\\n\\n`message()` accepts an optional `pattern` parameter of type `string` or `RegExp` object which filters out any messages that don’t match the pattern.\\n</div>\\n\\n```javascript\\n// This will match any message that contains 👋\\napp.message(':wave:', async ({ message, say }) => {\\n  await say(`Hello, <@${message.user}>`);\\n});\\n```\\n\\n<details class=\\\"secondary-wrapper\\\">\\n<summary class=\\\"section-head\\\" markdown=\\\"0\\\">\\n<h4 class=\\\"section-head\\\">Using a RegExp pattern</h4>\\n</summary>\\n\\n<div class=\\\"secondary-content\\\" markdown=\\\"0\\\">\\nA RegExp pattern can be used instead of a string for more granular matching.\\n\\nAll of the results of the RegExp match will be in `context.matches`.\\n</div>\\n\\n```javascript\\napp.message(/^(hi|hello|hey).*/, async ({ context, say }) => {\\n  // RegExp matches are inside of context.matches\\n  const greeting = context.matches[0];\\n\\n  await say(`${greeting}, how are you?`);\\n});\\n```\\n\\n</details>\"}}}"
  },
  "requestId": "87426979866799fdd0d929cb81a1830a"
}
    at errorHandler (/Users/runner/work/bolt-js/bolt-js/node_modules/contentful-management/dist/contentful-management.node.js:18124:17)
    at processTicksAndRejections (internal/process/task_queues.js:95:5)

Here is my code:

client.getSpace(spaceId)
          .then((space) => space.getEnvironment(envId))
          .then((environment) => environment.createEntry('page', {
            fields: {
              source: {
                'en-US': `https://github.com/${process.env.REPOSITORY}/blob/main/${path}`
              },
              markdown: {
                'en-US': fContent
              },
            }
        }))

@srajiang srajiang changed the title Invalid value "undefined" for header "X-Contentful-Content-Type" How to set a custom id for a new entry? Sep 2, 2021
@srajiang srajiang closed this as completed Sep 2, 2021
@ghost
Copy link

ghost commented Mar 1, 2024

What was the solution here?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant