New Record Creation Results in Empty Records

When creating a record from a from submission with the following code, a blank record is created in my development environment in DatoCMS that seems to only have a version number and a blank field for each of my model’s fields, ā€œnameā€ and ā€œemailā€.

Also, the blank records are appearing in the right environment, so I don’t think that’s an issue.

I confirmed that the formData holds the desired values and I am not getting any errors, whether in the client or local server console.

'use server'

import { buildClient } from "@datocms/cma-client-node";

const CMSEnvironment = process.env.VERCEL_ENV === "production" ? 'production' : 'development-01';

const Client = buildClient({apiToken: process.env.NEXT_DATOCMS_API_TOKEN || null, environment: CMSEnvironment});

export async function create(formData:any) {
  const { name, email } = formData;
  const data = await Client.items.create({
    item_type: { type: "item_type", id: "OHdmMsVMT0a9NZ4aS9gjag" },
    name,
    email
  });
 
  return JSON.parse(JSON.stringify(data.meta))
}

Hey @zacharydurland,

Hmm, sorry, it’s a bit hard to diagnose this without seeing what exactly {name, email} are and what’s getting sent over the wire. Could you please try to turn on a higher level of logging verbosity (like at least LogLevel.BODY) to better debug what the actual API request is?

Then if it’s not obvious from that request what’s wrong, could you please post it here (stripping away any sensitive auth headers, etc.) or email us the details at support@datocms.com?

Thank you!

Thank you for the response!

Although the above function receives the values for ā€œnameā€ and ā€œemailā€.

The readout seems to have empty data properties.

POST https://site-api.datocms.com/items
[1] {
  "data": {
    "type": "item",
    "attributes": {},
    "relationships": {
      "item_type": {
        "data": {
          "id": "OHdmMsVMT0a9NZ4aS9gjag",
          "type": "item_type"
        }
      }
    }
  }
}
[1] Status: 201 (Created)
[1] {
  "data": {
    "id": "A8VF40cpTEO-mt1um139ag",
    "type": "item",
    "attributes": {
      "name": "",
      "email": "",
      "updated_at": "2024-09-30T21:12:39.752-05:00",
      "created_at": "2024-09-30T21:12:39.745-05:00"
    },
    "relationships": {
      "item_type": {
        "data": {
          "id": "OHdmMsVMT0a9NZ4aS9gjag",
          "type": "item_type"
        }
      },
      "creator": {
        "data": {
          "id": "282504",
          "type": "access_token"
        }
      }
    },
    "meta": {
      "created_at": "2024-09-30T21:12:39.745-05:00",
      "updated_at": "2024-09-30T21:12:39.752-05:00",
      "published_at": "2024-09-30T21:12:39.786-05:00",
      "publication_scheduled_at": null,
      "unpublishing_scheduled_at": null,
      "first_published_at": "2024-09-30T21:12:39.786-05:00",
      "is_valid": true,
      "is_current_version_valid": true,
      "is_published_version_valid": true,
      "status": "published",
      "current_version": "WnYdH0H4SJSISmr_4od7vg",
      "stage": null
    }
  },
  "included": [
    {
      "id": "OHdmMsVMT0a9NZ4aS9gjag",
      "type": "item_type",
      "attributes": {
        "name": "Profile",
        "singleton": false,
        "sortable": false,
        "api_key": "profile",
        "ordering_direction": null,
        "ordering_meta": null,
        "tree": false,
        "modular_block": false,
        "draft_mode_active": false,
        "all_locales_required": false,
        "collection_appeareance": "table",
        "collection_appearance": "table",
        "has_singleton_item": false,
        "hint": null,
        "inverse_relationships_enabled": false
      },
      "relationships": {
        "fields": {
          "data": [
            {
              "id": "LA9DMHChRoqEUA1hzk7MUA",
              "type": "field"
            },
            {
              "id": "QSFa-89oQ1yhUJbhhMTKnA",
              "type": "field"
            }
          ]
        },
        "fieldsets": {
          "data": []
        },
        "singleton_item": {
          "data": null
        },
        "ordering_field": {
          "data": null
        },
        "title_field": {
          "data": {
            "id": "LA9DMHChRoqEUA1hzk7MUA",
            "type": "field"
          }
        },
        "image_preview_field": {
          "data": null
        },
        "excerpt_field": {
          "data": null
        },
        "workflow": {
          "data": null
        }
      },
      "meta": {
        "has_singleton_item": false
      }
    }
  ]
}

@zacharydurland,

Thank you for sharing that, and that’s really bizarre! Sorry for the hassle here… hopefully we can figure out what’s going on.

What is formData? Can you post an example of that object?

And this is on Next, right? I wonder if it might be accidentally caching the fetch… hmm.

Does it make a difference if you supply another fetch function, just as a test? Like https://www.datocms.com/docs/content-management-api/using-the-nodejs-clients#ponyfilling-fetch:

import { fetch } from '@whatwg-node/fetch';
const client = buildClient({ apiToken: '<YOUR_TOKEN>', fetchFn: fetch });

As a last-ditch effort, does it make any difference if you make a raw HTTP call instead of using the JS client? https://www.datocms.com/docs/content-management-api/resources/item/create#basic-example

Sorry for the seemingly random ideas here… just trying to figure out which part of the stack is acting up for you. This isn’t an issue I’ve seen from other customers so far, so it might take a bit of trial and error to diagnose. Sorry!