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.