【发布时间】:2021-04-02 22:59:24
【问题描述】:
我在做一个电子项目时遇到了这种情况。下面是一个 pouchDB put 函数,我正在尝试用它上传附件
我现在的代码是这样的:
testCasesDB.put({
_id: String(info.doc_count),
collectionID: String(collectionID),
name: String(tName),
description: String(tDescription),
performed: tPerform,
added: tAdd,
_attachments: {
testCaseFile: {
type: tAttachment.type,
data: tAttachment,
},
},
// ...
});
问题是我想检查变量 tAttachment 是否设置。如果不是,我不想在 pouchDB 中添加附件,如果设置了它,我希望它如上所述。为此,我通常会编写两个重复的代码并添加 _attachment 选项。我想知道是否有更好的方法来做到这一点。像这样的东西? (以下行不通):
testCasesDB.put({
_id: String(info.doc_count),
collectionID: String(collectionID),
name: String(tName),
description: String(tDescription),
performed: tPerform,
added: tAdd,
_attachments: {
testCaseFile: {
function() {
if (tAttachment) {
returnData = {
type: tAttachment.type,
data: tAttachment,
};
} else {
returnData = null;
}
return returnData;
},
},
},
});
【问题讨论】:
-
您的解决方案真的有效吗?对我来说似乎是狡猾的代码
-
@badsyntax 不,第二个代码代表我打算做什么。我目前的解决方案是检查选项之外的附件变量
标签: javascript performance electron pouchdb