我有一个内容类型“blogpost”,当前包含对另一个名为“image”的内容类型的引用。内容类型“图像”具有指向 Assets (图像文件)的链接。
现在我想创建一个迁移脚本,直接从“blogpost”条目链接到 Assets 。
Current content model:
entry 'My blog post'
→ field 'image'
→ link to entry 'My image entry'
→ field 'image'
→ link to asset 'My image asset'
Transform to:
entry 'My blog post'
→ field 'mainImage'
→ link to asset 'My image asset'
这是迄今为止的迁移:
module.exports = function (migration, { makeRequest }) {
// Create new mainImage field in blogpost content type
const blogpost = migration.editContentType('blogpost');
blogpost
.createField('mainImage')
.name('Main Image')
.type('Link')
.linkType('Asset')
.validations([
{
'linkMimetypeGroup': ['image']
}
]);
migration.transformEntries({
contentType: 'blogpost',
from: ['image'],
to: ['mainImage'],
transformEntryForLocale: async function (fromFields, currentLocale) {
// Get the the entry of type 'image' from the blogpost's 'image' field
const imageEntryId = fromFields.image[currentLocale].sys.id;
const imageEntry = await makeRequest({
method: 'GET',
url: `/entries/${imageEntryId}`
});
return { mainImage: imageEntry.fields.image[currentLocale] };
}
});
}
我尝试将“mainImage”字段映射到 imageEntry.fields.image 和 imageAsset 的不同部分,但我似乎无法正确映射。
当我运行迁移脚本时,我通常会收到此错误消息。我的区域设置是“nb-NO”:
类型错误:无法读取未定义的属性(读取“nb-NO”)
最佳答案
我终于弄清楚我做错了什么以及上面的错误消息意味着什么。
迁移脚本本身按预期工作,但由于没有图像的博客文章条目而崩溃。
当 fromFields.image 为 null 时,此行崩溃:
const imageEntryId = fromFields.image[currentLocale].sys.id;
我用以下空检查替换了它:
if (fromFields.image === null || typeof (fromFields.image) === "undefined") {
return;
}
const imageEntryId = fromFields.image[currentLocale].sys.id;
关于内容迁移 : How to migrate a link to an asset?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71640491/