我是新手做出反应和jsx ...有关我的有效负载语法有什么问题吗?如果type
为internal
我想发送media_file
,或type
是external
,我想发送url
。
const createMedia = (payload) => {
const data = excludeObjNullValue({
name: payload.name,
content_id: payload.contentId,
destination_id: payload.destinationId,
task_id: payload.taskId,
type: payload.type,
if(type === 'internal') {
media_file: payload.mediaFile;
} else {
url: payload.url;
}
}, true);
const formData = new FormData();
for (const [k, v] of Object.entries(data)) {
formData.append(k, v);
}
return request.postFormData('/media', formData);
};
答案 0 :(得分:3)
您无法在对象定义中使用if/else
。您应首先创建一个对象,然后使用if/else
向该属性添加属性。
let obj = {
name: payload.name,
content_id: payload.contentId,
destination_id: payload.destinationId,
task_id: payload.taskId,
type: payload.type
}
if(type === 'internal') {
obj.media_file = payload.mediaFile;
} else {
obj.url = payload.url;
}
const data = excludeObjNullValue(obj, true);