我有一个PayloadCMS错误。ValidationError:以下字段无效:email

huangapple go评论218阅读模式
英文:

I have a PayloadCMS error. ValidationError: The following field is invalid: email

问题

我使用npx create-payload-app创建了一个负载应用程序,并选择了使用空白模板。

我有以下错误:ValidationError: 以下字段无效:电子邮件

src/seed/index.ts

  1. import { Payload } from 'payload';
  2. import { customerForm } from './customerForm';
  3. export const seed = async (payload: Payload) => {
  4. const customerFormJSON = JSON.parse(JSON.stringify(customerForm));
  5. const { id: customerFormID } = await payload.create({
  6. collection: 'customer',
  7. data: customerFormJSON,
  8. });
  9. };

src/seed/customerForm.ts

  1. export const customerForm = {
  2. id: '63c0651b132c8e2783f8dcae',
  3. updatedAt: '2023-01-12T21:25:41.113Z',
  4. createdAt: '2022-12-28T20:48:53.181Z',
  5. title: '客户表单',
  6. fields: [
  7. {
  8. name: 'firstName',
  9. label: '名字',
  10. width: 50,
  11. required: true,
  12. id: '63adaaba5236fe69ca8973f8',
  13. blockName: '名字',
  14. blockType: '文本',
  15. },
  16. {
  17. name: 'lastName',
  18. label: '姓氏',
  19. width: 50,
  20. required: true,
  21. id: '63bf4b1fd69cef4f34272f9a',
  22. blockName: '姓氏',
  23. blockType: '文本',
  24. },
  25. {
  26. name: 'email',
  27. label: '电子邮件',
  28. width: 100,
  29. required: true,
  30. id: '63bf4b1fd69cef4f34272f9b',
  31. blockName: '电子邮件',
  32. blockType: '文本',
  33. },
  34. ],
  35. redirect: {},
  36. };

src/collections/Customers.ts

  1. import { CollectionConfig, Block } from 'payload/types';
  2. export const Customers: CollectionConfig = {
  3. slug: 'customer',
  4. // auth: true,
  5. fields: [
  6. {
  7. name: 'firstName',
  8. type: '文本',
  9. // required: true,
  10. },
  11. {
  12. name: 'lastName',
  13. type: '文本',
  14. // required: true,
  15. },
  16. {
  17. name: 'email',
  18. type: '文本',
  19. // required: true,
  20. },
  21. ]
  22. }

src/server.ts

  1. import express from 'express';
  2. import payload from 'payload';
  3. import { seed } from './seed';
  4. require('dotenv').config();
  5. const app = express();
  6. // 重定向根目录到管理员面板
  7. app.get('/', (_, res) => {
  8. res.redirect('/admin');
  9. });
  10. const start = async () => {
  11. // 初始化 Payload
  12. await payload.init({
  13. secret: process.env.PAYLOAD_SECRET,
  14. mongoURL: process.env.MONGODB_URI,
  15. express: app,
  16. email: {
  17. fromName: '管理员',
  18. fromAddress: 'admin@example.com',
  19. logMockCredentials: true,
  20. },
  21. onInit: async () => {
  22. payload.logger.info(`Payload 管理员 URL: ${payload.getAdminURL()}`)
  23. },
  24. })
  25. // 在此添加您自己的 express 路由
  26. if (process.env.PAYLOAD_SEED === 'true') {
  27. payload.logger.info('---- 正在填充数据库 ----');
  28. await seed(payload);
  29. }
  30. app.listen(3000);
  31. }
  32. start();

当我在customers.ts中取消注释(required: true)时,我得到以下错误:ValidationError: 以下字段无效:名字,姓氏

当我在customers.ts中注释掉(required: true)时,我得到以下错误:ValidationError: 以下字段无效:电子邮件

注意:我只想使用payload.create来创建数据。任何帮助将不胜感激。

英文:

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

  1. import { Payload } from 'payload';
  2. import { customerForm } from './customerForm';
  3. export const seed = async (payload: Payload) => {
  4. const customerFormJSON = JSON.parse(JSON.stringify(customerForm));
  5. const { id: customerFormID } = await payload.create({
  6. collection: 'customer',
  7. data: customerFormJSON,
  8. });
  9. };

src/seed/customerForm.ts

  1. export const customerForm = {
  2. id: '63c0651b132c8e2783f8dcae',
  3. updatedAt: '2023-01-12T21:25:41.113Z',
  4. createdAt: '2022-12-28T20:48:53.181Z',
  5. title: 'Customer Form',
  6. fields: [
  7. {
  8. name: 'firstName',
  9. label: 'First name',
  10. width: 50,
  11. required: true,
  12. id: '63adaaba5236fe69ca8973f8',
  13. blockName: 'first-name',
  14. blockType: 'text',
  15. },
  16. {
  17. name: 'lastName',
  18. label: 'Last name',
  19. width: 50,
  20. required: true,
  21. id: '63bf4b1fd69cef4f34272f9a',
  22. blockName: 'last-name',
  23. blockType: 'text',
  24. },
  25. {
  26. name: 'email',
  27. label: 'Email',
  28. width: 100,
  29. required: true,
  30. id: '63bf4b1fd69cef4f34272f9b',
  31. blockName: 'email',
  32. blockType: 'text',
  33. },
  34. ],
  35. redirect: {},
  36. };

src/collections/Customers.ts

  1. import { CollectionConfig, Block } from 'payload/types';
  2. export const Customers: CollectionConfig = {
  3. slug: 'customer',
  4. // auth: true,
  5. fields: [
  6. {
  7. name: 'firstName',
  8. type: 'text',
  9. // required: true,
  10. },
  11. {
  12. name: 'lastName',
  13. type: 'text',
  14. // required: true,
  15. },
  16. {
  17. name: 'email',
  18. type: 'text',
  19. // required: true,
  20. },
  21. ]
  22. }

src/server.ts

  1. import express from 'express';
  2. import payload from 'payload';
  3. import { seed } from './seed'
  4. require('dotenv').config();
  5. const app = express();
  6. // Redirect root to Admin panel
  7. app.get('/', (_, res) => {
  8. res.redirect('/admin');
  9. });
  10. const start = async () => {
  11. // Initialize Payload
  12. await payload.init({
  13. secret: process.env.PAYLOAD_SECRET,
  14. mongoURL: process.env.MONGODB_URI,
  15. express: app,
  16. email: {
  17. fromName: 'Admin',
  18. fromAddress: 'admin@example.com',
  19. logMockCredentials: true,
  20. },
  21. onInit: async () => {
  22. payload.logger.info(`Payload Admin URL: ${payload.getAdminURL()}`)
  23. },
  24. })
  25. // Add your own express routes here
  26. if (process.env.PAYLOAD_SEED === 'true') {
  27. payload.logger.info('---- SEEDING DATABASE ----');
  28. await seed(payload);
  29. }
  30. app.listen(3000);
  31. }
  32. start();

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

when I comment (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

答案1

得分: 3

我找到了问题。
问题是我删除/注释了字段email、firstName或lastName上的唯一约束,但忘记更新数据库。
即使我从Payload CMS集合配置中删除了email、firstName、lastName或name的唯一约束,如果在我的更改后未正确更新,MongoDB可能仍会在数据库级别执行它。我手动检查了我的MongoDB集合的索引,以确认'name'字段上的唯一约束是否仍然存在。

像这样:

  1. mydb> db.generics.getIndexes()
  2. [
  3. { v: 2, key: { _id: 1 }, name: '_id_' },
  4. {
  5. v: 2,
  6. key: { name: 1 },
  7. name: 'name_1',
  8. background: true,
  9. unique: true
  10. }
  11. ]
  12. mydb> db.generics.dropIndex("name_1")
  13. { nIndexesWas: 2, ok: 1 }
英文:

I figured the issue.
The issue was that I deleted/commented 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:

  1. mydb> db.generics.getIndexes()
  2. [
  3. { v: 2, key: { _id: 1 }, name: '_id_' },
  4. {
  5. v: 2,
  6. key: { name: 1 },
  7. name: 'name_1',
  8. background: true,
  9. unique: true
  10. }
  11. ]
  12. mydb> db.generics.dropIndex("name_1")
  13. { nIndexesWas: 2, ok: 1 }

huangapple
  • 本文由 发表于 2023年6月16日 03:03:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/76484774.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定