Cannot submit form data to DatoCMS using Gatsby.js

Viewed 179

I am submitting form data using this:

const { SiteClient } = require("datocms-client")
const client = new SiteClient("APIKEYREMOVED")
export default async function createRecord(fields) {
  console.log('@submit function ',fields)
  const record = await client.items.create(fields)
  console.log(record)
}

This is the error information, but I can't make heads or tails of it.

Gatsby form submit error

The form's handle submit function successfully call createRecord() from the form page. So, problem would seem to be from the snippet provided.

I sourced said snippet from here: DatoCMS create record simple example

1 Answers

I think you are missing the relationship between your form and the content model in DatoCMS. According to the docs:

Suppose our project contains a "Dog" model (ID: 1234, API key: dog) with the >following fields:

Field API Key Field Type
name Single-line String
breed Single-line String
description Multiple-paragraph text
age Integer
const { SiteClient } = require("datocms-client");
const client = new SiteClient("YOUR-API-TOKEN");
async function createRecord() {
  const record = await client.items.create({
    itemType: "1234", // model ID
    name: "Gigio",
    breed: "Labrador",
    description: "Very friendly and calm.\nI love it.",
    age: 4,
  });
  console.log(record);
}
createRecord();

In your case, it seems that you are not matching the itemType, which is the model ID, as the promise output suggests in your console.log.

Related