admin管理员组文章数量:1402935
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 badges1 Answer
Reset to default 8I 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 }
版权声明:本文标题:javascript - I have a PayloadCMS error. ValidationError: The following field is invalid: email - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744329147a2600876.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论