"ValidationError: The following field is invalid" during migration

default discord avatar
marlon_90585
2 weeks ago
2

Hey there, I am trying to create a migration using payload. I tried many different things so for now my current state is that I'm reading the old object and saving the transformed object to a different collection.


My first question is how to do a migration properly and my second one is why I'm getting the error attached below.



Code:


const questions = await payload.find({
    collection: 'questions',
  });
  do {
    const docs = questions.docs;

    const newQuestions = (docs as unknown as Question[]).map(
      (q: Question): QuestionsNew => {
        return {
          id: crypto.randomUUID(),
          question: q.question,
          type:
            q.questionType === 'a'
              ? 'a'
              : 'b',
          category: q.category,
          possibleAnswers: getAnswerArray(q),
          solution:
            q.solution_numerical ??
            parseInt(q.solution_multichoice),
          updatedAt: new Date().toString(),
          createdAt: q.createdAt,
        };
      },
    );
    for (const newQuestion of newQuestions) {
      console.log(JSON.stringify(newQuestion));
      await payload.create({
        collection: 'questionsNew',
        data: newQuestion,
      });
    }
  } while (questions.nextPage);


Error (shortened):


[16:01:15] ERROR (payload): Error running migration ***
    [...]
node:internal/process/promises:288
            triggerUncaughtException(err, true /* fromPromise */);
            ^

ValidationError: The following field is invalid: question
    at beforeChange (C:\***\backend\node_modules\payload\dist\fields\hooks\beforeChange\index.js:38:15)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async create (C:\***\backend\node_modules\payload\dist\collections\operations\create.js:135:35) {
  data: [ { field: 'question', message: 'This field is required.' } ],
  isOperational: true,
  isPublic: false,
  status: 400
}

Node.js v18.13.0


EDIT

: I'm using MongoDB.



"ValidationError: The following field is invalid" during migration

  • discord user avatar
    denolfe
    Payload Team
    2 weeks ago

    Can you log out the values for the

    question

    ? Sounds like the value may not exist or be what you're expecting.



    Looks like you already are. What were the values?

  • default discord avatar
    marlon_90585
    2 weeks ago

    I have already tried to log them but I could not spot anything unusual with them.


    {
      "id": "8f6444ce-5063-4137-a464-1bd54958d261",
      "question": [
        {
          "children": [
            {
              "text": "Die Reihenfolge der Zahlen erfolgt nach bestimmten mathematischen Regeln. Ersetzen Sie das Fragezeichen mit der richtigen Zahl."
            }
          ]
        },
        {
          "children": [
            {
              "text": ""
            }
          ]
        },
        {
          "children": [
            {
              "text": "Zahlenreihe"
            }
          ]
        },
        {
          "children": [
            {
              "text": "3\t9\t6\t9\t27\t?"
            }
          ]
        },
        {
          "children": [
            {
              "text": ""
            }
          ]
        }
      ],
      "type": "numerical",
      "category": "652e81e221293f3814b0eec5",
      "possibleAnswers": [],
      "solution": 24,
      "updatedAt": "Tue Nov 14 2023 17:01:15 GMT+0100 (Mitteleuropäische Normalzeit)",
      "createdAt": "2023-10-17T12:49:24.065Z"
    }


    this is one object from the list I fetched from the database (the one where the exception is thrown)



    nvm I logged out the wrong object. I will log out the right one and edit my message accordingly. please wait a few moments.


    seems like I'm not very concentrated right now. It was in fact the right one.



    do you need the collection code?



    NewQuestions.ts (collection where the new questions are inserted to)


    const Questions: CollectionConfig = {
      slug: 'questionsNew',
      labels: {
        singular: 'Frage',
        plural: 'Fragen',
      },
      access: {
        read: ({ req: { user } }) => {
          return true;
        },
      },
      admin: {
        description: 'Fragen für den Logiktest',
        useAsTitle: 'category',
      },
      fields: [
        {
          name: 'question',
          label: 'Frage',
          required: true,
          type: 'richText',
        },
        {
          name: 'type',
          label: 'Fragentype',
          type: 'select',
          required: true,
          defaultValue: 'multipleChoice',
          options: [
            {
              label: 'Multiple Choice',
              value: 'multipleChoice',
            },
            {
              label: 'Numerisch',
              value: 'numerical',
            },
          ],
        },
        {
          name: 'category',
          label: 'Kategorie',
          type: 'relationship',
          relationTo: 'category',
          hasMany: false,
          required: true,
          maxDepth: 0,
        },
        {
          name: 'possibleAnswers',
          label: 'Antwortmoeglichkeiten',
          type: 'array',
          fields: [
            {
              name: 'image',
              label: 'Grafik',
              type: 'upload',
              relationTo: 'media',
            },
            {
              name: 'text',
              label: 'Text',
              type: 'textarea',
            },
          ],
          required: true,
          admin: {
            condition: (data) => data.type !== 'numerical',
          },
        },
        {
          name: 'solution',
          label: 'Lösung',
          type: 'number',
          required: true,
        },
      ],
    };


    I found the issue. I tried to save slateeditor data while using lexical 🙂

Open the post
Continue the discussion in Discord
Like what we're doing?
Star us on GitHub!

Star

Connect with the Payload Community on Discord

Discord

online

Can't find what you're looking for?

Get help straight from the Payload team with an Enterprise License.