使用 FilePond 我的隐藏输入值 ="undefined" 和 JSON 响应

Using FilePond my hidden input value="undefined" with JSON response

一切都在后台正确上传,我在 JSON、

中收到服务器的响应

{"success":true,"filename":"image1.png","assetId":946}

但是当使用 > docs

时,它不会用 assetId 填充隐藏字段

onload: (response) => response.assetId

如果我删除 onload,那么默认功能 returns 纯文本,然后我的隐藏输入将在值中显示整个 JSON 响应

<input type="hidden" name="image-upload" value="{"success":true,"filename":"image1.png","assetId":946}">

另外,上传成功后是否也可以更改隐藏的输入名称?作为 Craft,CMS 需要它应该 saved/linked 的字段?烦人的是,我需要一个名称供 Craft 控制器上传到系统,另一个名称用于将其保存到条目中的正确字段。

FilePond.parse(document.body);

FilePond.setOptions({
    allowDrop: true,
    allowReplace: true,
    instantUpload: true,
    server: {
        url: 'https://example.com/',
        process: {
            url: './actions/assets/upload',
            ondata: (formData) => {
                formData.append('folderId', '8');
                return formData;
            },
            onload: (response) => response.assetId,          
        }
    }  
});

我自己解决了 - 我会离开这里,因为它对其他人有帮助。

FilePond.parse(document.body);

    FilePond.setOptions({
        allowDrop: true,
        allowReplace: true,
        instantUpload: true,
        server: {
            url: 'https://example.com/',
            process: {
                url: './actions/assets/upload',
                ondata: (formData) => {
                    formData.append('folderId', '8');
                    formData.append('fieldId', '18');
                    formData.append('elementId', '1');
                    return formData;
                },
                onload: (response) => {
                    var json = (response);
                    var obj = JSON.parse(json);
                    var id = obj.assetId;
                    var input = document.createElement("input");
                    input.type = "hidden";
                    input.name = "fields[cover][]";
                    input.value = id;
                    document.getElementById("entry-form").appendChild(input);
                },  

            }
        }  
    });