Javascript 在DatoCMS中创建记录时如何包含图像
我的应用程序通过javascript客户端成功提交单行文本,如下所示。我不明白如何实际发送图像数据。单个资产的可接受字段似乎没有提及实际的图像数据 看起来需要多个http请求才能完成,但再一次,我的文件输入数据去了哪里 ----更新---- 看起来必须先上载图像才能获取ID,这是创建新记录时使用的ID。您不能同时上载图像和文本 我当前遇到以下错误: 参考:Javascript 在DatoCMS中创建记录时如何包含图像,javascript,graphql,gatsby,Javascript,Graphql,Gatsby,我的应用程序通过javascript客户端成功提交单行文本,如下所示。我不明白如何实际发送图像数据。单个资产的可接受字段似乎没有提及实际的图像数据 看起来需要多个http请求才能完成,但再一次,我的文件输入数据去了哪里 ----更新---- 看起来必须先上载图像才能获取ID,这是创建新记录时使用的ID。您不能同时上载图像和文本 我当前遇到以下错误: 参考: 根据DatoCMS文档,我认为您的createRecord函数应该如下所示: const { SiteClient } = requir
根据DatoCMS文档,我认为您的
createRecord
函数应该如下所示:
const { SiteClient } = require("datocms-client")
const client = new SiteClient("API_KEY")
export default async function createRecord({ firstName, lastName, email, profileImage, slug}) {
console.log('@createProfile',profileImage)
// here you are creating the path for local image
const path = await client.createUploadPath(profileImage);
// you can then use the returned path to create a new upload:
const upload = await client.uploads.create({ path });
const record = await client.items.create({
itemType: "679748",
path, // alternatively use upload
firstName,
lastName,
email,
slug
})
return record
}
文件中对资产的结构要求不够明确,您可能需要:
const record = await client.items.create({
itemType: "679748",
profileImage: {
// in this case we're just passing the upload ID, as the
// asset's defaults for alt, title, etc. are fine:
uploadId: upload.id,
},
firstName,
lastName,
email,
slug
})
答案严格链接到您的内容模型,但基于和,但在这两种情况下,您都缺少
createUploadPath
helper,从这里开始,其余的都是尝试和错误。谢谢,我了解到我需要首先使用createUpload上传图像,获取图像ID,然后使用createRecord创建一个新记录,并包含新上载的图像ID。执行此操作时出现此错误:无法读取undefined的属性“onProgress”。所以,我不知道我的错误是什么。你能再调试一下吗?它是否达到(或结束)以下承诺:const path=wait client.createUploadPath(profileImage)
?
const record = await client.items.create({
itemType: "679748",
profileImage: {
// in this case we're just passing the upload ID, as the
// asset's defaults for alt, title, etc. are fine:
uploadId: upload.id,
},
firstName,
lastName,
email,
slug
})