最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - I have a PayloadCMS error. ValidationError: The following field is invalid: email - Stack Overflow

matteradmin1PV0评论

I create a payload app using npx create-payload-app and I chose to use blank template

I have this. ValidationError: The following field is invalid: email

src/seed/index.ts

import { Payload } from 'payload';
import { customerForm } from './customerForm';

export const seed = async (payload: Payload) => {
  const customerFormJSON = JSON.parse(JSON.stringify(customerForm));

  const { id: customerFormID } = await payload.create({
    collection: 'customer', 
    data: customerFormJSON,
  });

};

src/seed/customerForm.ts

export const customerForm = {
    id: '63c0651b132c8e2783f8dcae',
    updatedAt: '2023-01-12T21:25:41.113Z',
    createdAt: '2022-12-28T20:48:53.181Z',
    title: 'Customer Form',
    fields: [
      {
        name: 'firstName',
        label: 'First name',
        width: 50,
        required: true,
        id: '63adaaba5236fe69ca8973f8',
        blockName: 'first-name',
        blockType: 'text',
      },
      {
        name: 'lastName',
        label: 'Last name',
        width: 50,
        required: true,
        id: '63bf4b1fd69cef4f34272f9a',
        blockName: 'last-name',
        blockType: 'text',
      },
      {
        name: 'email',
        label: 'Email',
        width: 100,
        required: true,
        id: '63bf4b1fd69cef4f34272f9b',
        blockName: 'email',
        blockType: 'text',
      },
    ],
    redirect: {},
  };
  

src/collections/Customers.ts

import { CollectionConfig, Block } from 'payload/types';

export const Customers: CollectionConfig = {
  slug: 'customer',
  // auth: true,
  fields: [
    {
      name: 'firstName',
      type: 'text',
      // required: true,
    },
    {
      name: 'lastName',
      type: 'text',
      // required: true,
    },
    {
      name: 'email',
      type: 'text',
      // required: true,
    },
  ]
}

src/server.ts

import express from 'express';
import payload from 'payload';
import { seed } from './seed'


require('dotenv').config();
const app = express();

// Redirect root to Admin panel
app.get('/', (_, res) => {
  res.redirect('/admin');
});

const start = async () => {
  // Initialize Payload
  await payload.init({
    secret: process.env.PAYLOAD_SECRET,
    mongoURL: process.env.MONGODB_URI,
    express: app,
    email: {
      fromName: 'Admin',
      fromAddress: '[email protected]',
      logMockCredentials: true,
    },
    onInit: async () => {
      payload.logger.info(`Payload Admin URL: ${payload.getAdminURL()}`)
    },
  })

  // Add your own express routes here

  if (process.env.PAYLOAD_SEED === 'true') {
    payload.logger.info('---- SEEDING DATABASE ----');
    await seed(payload);
  }

  app.listen(3000);
}

start();

When I unment (required: true) in customers.ts I get this error: ValidationError: followingFieldsInvalid firstName, lastName

when I ment (required: true) in customers.ts ValidationError: The following field is invalid: email

NB: I only want to use payload.create for creating data.

Any help is appreciated

I create a payload app using npx create-payload-app and I chose to use blank template

I have this. ValidationError: The following field is invalid: email

src/seed/index.ts

import { Payload } from 'payload';
import { customerForm } from './customerForm';

export const seed = async (payload: Payload) => {
  const customerFormJSON = JSON.parse(JSON.stringify(customerForm));

  const { id: customerFormID } = await payload.create({
    collection: 'customer', 
    data: customerFormJSON,
  });

};

src/seed/customerForm.ts

export const customerForm = {
    id: '63c0651b132c8e2783f8dcae',
    updatedAt: '2023-01-12T21:25:41.113Z',
    createdAt: '2022-12-28T20:48:53.181Z',
    title: 'Customer Form',
    fields: [
      {
        name: 'firstName',
        label: 'First name',
        width: 50,
        required: true,
        id: '63adaaba5236fe69ca8973f8',
        blockName: 'first-name',
        blockType: 'text',
      },
      {
        name: 'lastName',
        label: 'Last name',
        width: 50,
        required: true,
        id: '63bf4b1fd69cef4f34272f9a',
        blockName: 'last-name',
        blockType: 'text',
      },
      {
        name: 'email',
        label: 'Email',
        width: 100,
        required: true,
        id: '63bf4b1fd69cef4f34272f9b',
        blockName: 'email',
        blockType: 'text',
      },
    ],
    redirect: {},
  };
  

src/collections/Customers.ts

import { CollectionConfig, Block } from 'payload/types';

export const Customers: CollectionConfig = {
  slug: 'customer',
  // auth: true,
  fields: [
    {
      name: 'firstName',
      type: 'text',
      // required: true,
    },
    {
      name: 'lastName',
      type: 'text',
      // required: true,
    },
    {
      name: 'email',
      type: 'text',
      // required: true,
    },
  ]
}

src/server.ts

import express from 'express';
import payload from 'payload';
import { seed } from './seed'


require('dotenv').config();
const app = express();

// Redirect root to Admin panel
app.get('/', (_, res) => {
  res.redirect('/admin');
});

const start = async () => {
  // Initialize Payload
  await payload.init({
    secret: process.env.PAYLOAD_SECRET,
    mongoURL: process.env.MONGODB_URI,
    express: app,
    email: {
      fromName: 'Admin',
      fromAddress: '[email protected]',
      logMockCredentials: true,
    },
    onInit: async () => {
      payload.logger.info(`Payload Admin URL: ${payload.getAdminURL()}`)
    },
  })

  // Add your own express routes here

  if (process.env.PAYLOAD_SEED === 'true') {
    payload.logger.info('---- SEEDING DATABASE ----');
    await seed(payload);
  }

  app.listen(3000);
}

start();

When I unment (required: true) in customers.ts I get this error: ValidationError: followingFieldsInvalid firstName, lastName

when I ment (required: true) in customers.ts ValidationError: The following field is invalid: email

NB: I only want to use payload.create for creating data.

Any help is appreciated

Share Improve this question asked Jun 15, 2023 at 19:03 ByusaByusa 3,0972 gold badges22 silver badges25 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 8

I figured the issue. The issue was that I deleted/mented the unique constraint on the on the fields email, firstName or lastName but forgot to update the database. Even though I removed the unique constraint from your Payload CMS collection configuration (for email, firstName, lastName, or name) , MongoDB might still enforce it at the database level if it wasn't properly updated after my changes. I manually inspected my MongoDB collection's indexes to confirm whether the unique constraint on the 'name' field still exists.

like this:

mydb> db.generics.getIndexes()
[
  { v: 2, key: { _id: 1 }, name: '_id_' },
  {
    v: 2,
    key: { name: 1 },
    name: 'name_1',
    background: true,
    unique: true
  }
]
mydb> db.generics.dropIndex("name_1")
{ nIndexesWas: 2, ok: 1 }
Post a comment

comment list (0)

  1. No comments so far