javascript - 我遇到 PayloadCMS 错误。验证错误: The following field is invalid: email

标签 javascript node.js typescript content-management-system

我使用以下命令创建了一个有效负载应用程序 npx 创建有效负载应用程序 我选择使用空白模板

我有这个。验证错误:以下字段无效:电子邮件

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: '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="bfdedbd2d6d1ffdac7ded2cfd3da91dcd0d2" rel="noreferrer noopener nofollow">[email protected]</a>',
      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();

当我在customers.ts中取消注释(必需:true)时 我收到此错误:ValidationError:followingFieldsInvalid firstName,lastName

当我在customers.ts中评论(必需:true)时 验证错误:以下字段无效:电子邮件

注意:我只想使用 Payload.create 来创建数据。

感谢任何帮助

最佳答案

我想到了这个问题。 问题是我删除/注释了电子邮件、名字或姓氏字段上的唯一约束,但忘记更新数据库。 即使我从您的 Payload CMS 集合配置(针对电子邮件、firstName、lastName 或 name)中删除了唯一约束,如果我的更改后未正确更新,MongoDB 可能仍会在数据库级别强制执行它。我手动检查了 MongoDB 集合的索引,以确认“名称”字段上的唯一约束是否仍然存在。

像这样:

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 - 我遇到 PayloadCMS 错误。验证错误: The following field is invalid: email,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76484774/

相关文章:

javascript - 输入字段值何时等于 '' 、 null 或 undefined

php - Jquery AJAX 成功但未给出响应

javascript - 延迟 Node js中的每个循环迭代,异步

javascript - 如果未提供,TypeScript 将参数设置为可选

angular - 使用 RXJS 继续以 Angular 触发 http 调用,直到满足条件

javascript - jQuery 函数在 $(window).resize() 或 $(document).ready() 上执行,但不能同时执行

javascript - 为什么这两个空数组的填充方式不同?

node.js - 当 npm 审计修复/更新/收缩包装手动更改不起作用时,如何更新 node-sass 的嵌套包 ansi-regex?

javascript - Node :Error 401(Unauthorized) i m using passport-jwt

html - 如何在编译时以 Angular 检查不正确的属性名称