如果不添加附件,PouchDB 代码将无法运行
PouchDB code won't work without adding attachment
我正在构建一个应用程序来存储信息项。目前有 3 个输入字段(标题、描述和文件输入)。
当我填写所有三个字段并调用 addItem() 函数时,它起作用了。但是当我将输入字段留空时,该函数不会将其添加到数据库中。
有没有办法告诉文件输入字段不是必需的?
我的javascript代码:
function addItem() {
//get file
var inputFile = document.querySelector('#inputFile');
var getFile = inputFile.files[0];
//get info
var title = document.getElementById('itemTitle').value;
var desc = document.getElementById('itemDesc').value;
//add
locallp.put({
_id: new Date().toISOString(),
title: title,
description: desc,
_attachments: {
"file": {
content_type: getFile.type,
data: getFile
}
}
}).then(function(){
console.log("Added to the database");
location.href = "menu.html";
}).catch(function(err){
console.log(err);
});
}
一些额外的信息,我正在使用 Cordova 构建应用程序。我的数据库是 PouchDB 并通过 couchperuser 连接到 CouchDB 服务器。
令我惊讶的是,即使您不提供文件,您的第二行 (inputFile.files[0]
) 也能正常工作。无论如何,如果没有文件,我确定 getFile.type
方法调用将失败。
您需要在此方法中添加一些逻辑,以便它根据是否提供文件来做两件不同的事情。如果有那么它会做你所拥有的,如果没有那么它不会尝试添加 _attachments
并且可能也会跳过 getFile
的整个设置。
我正在构建一个应用程序来存储信息项。目前有 3 个输入字段(标题、描述和文件输入)。
当我填写所有三个字段并调用 addItem() 函数时,它起作用了。但是当我将输入字段留空时,该函数不会将其添加到数据库中。
有没有办法告诉文件输入字段不是必需的?
我的javascript代码:
function addItem() {
//get file
var inputFile = document.querySelector('#inputFile');
var getFile = inputFile.files[0];
//get info
var title = document.getElementById('itemTitle').value;
var desc = document.getElementById('itemDesc').value;
//add
locallp.put({
_id: new Date().toISOString(),
title: title,
description: desc,
_attachments: {
"file": {
content_type: getFile.type,
data: getFile
}
}
}).then(function(){
console.log("Added to the database");
location.href = "menu.html";
}).catch(function(err){
console.log(err);
});
}
一些额外的信息,我正在使用 Cordova 构建应用程序。我的数据库是 PouchDB 并通过 couchperuser 连接到 CouchDB 服务器。
令我惊讶的是,即使您不提供文件,您的第二行 (inputFile.files[0]
) 也能正常工作。无论如何,如果没有文件,我确定 getFile.type
方法调用将失败。
您需要在此方法中添加一些逻辑,以便它根据是否提供文件来做两件不同的事情。如果有那么它会做你所拥有的,如果没有那么它不会尝试添加 _attachments
并且可能也会跳过 getFile
的整个设置。