再次使用相同功能时,不会将记录添加到PouchDB中 [英] Doesn't add records into PouchDB when used same function over again

查看:87
本文介绍了再次使用相同功能时,不会将记录添加到PouchDB中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个包含用户及其数据的数据库。奇怪的是,当我第三次尝试时,它没有 put()新变量。为此,我创建了一个本地数据库 dblocal 并将此数据库复制到名为 dbremote 的远程数据库。首先,我创建一个带有一个变量的文档。

I'm trying to create a database with "users" and their data in it. Strangely it doesn't put() new variables in it when I try to for the third time. To do all this I create a local database dblocal and replicate this DB to the remote db called dbremote. At first I create a document with one variable.

function newuser() {
    if (window.document.consent_form.consent_to_share.value) {
        var id = "p" + Date.now() + "-" + Math.floor(Math.random() * 10000);
        var dblocal = new PouchDB(id);
        var consenttoshare = window.document.consent_form.consent_to_share.value;
        document.cookie = id;
        var dbremote = 'http://localhost:5984/experiment';
        dblocal.put({
            _id: id,
            consent: consenttoshare
        });
        dblocal.replicate.to(dbremote, {live: true});
    }
}

这一切都很好,在另一个js文件中,我m尝试通过执行以下函数 putdb()向同一文档添加变量。我以以下方式执行此操作(如他们的文档中所述是正确的方式):

This all worked well, in another js file I'm trying to add a variable to the same document by executing the following function putdb(). Im doing this in the following way (as said in their documentation is the right way):

function putdb () {
    if (document.cookie){
        var id = document.cookie;
        var loggedin = "True";

        var dblocal = new PouchDB(id);
        dblocal.get(id).then(function (doc) {
            doc.loggedin = loggedin;
            return dblocal.put(doc);
        }).then(function () {
            return dblocal.get(id);
        }).then(function (doc) {
            console.log(doc);
            var dbremote = 'http://localhost:5984/experiment';
            dblocal.replicate.to(dbremote, {live: true});
        });
    }
}

此操作成功添加了变量根据需要登录到文档。但是,在尝试第三次向该文档添加信息(再次在另一个js文件中)时,没有任何反应。我使用了与以前完全相同的方法,但只使用了不同的变量。

This succesfully added the variable loggedin to the document as I wanted. However upon trying to add information to this document for the third time (again in another js file), nothing happens. I used exactly the same approach as before but only use different variables.

function putdb (checked) {
    if (document.cookie) {
        var id = document.cookie;
        var checkedlist = [];    
        for (i = 0; i < checked; i++) {
            checkedlist.push($("input[type=checkbox]:checked")[i].value)
        }
        var playlistname = document.getElementById("playlistname").value;

        var dblocal = new PouchDB(id);
        dblocal.get(id).then(function (doc) {
            doc.checkedlist = checkedlist;
            doc.playlistname = playlistname;
            return dblocal.put(doc);
        }).then(function () {
            return dblocal.get(id);
        }).then(function (doc) {
            console.log(doc);
            var dbremote = 'http://localhost:5984/experiment';
            dblocal.replicate.to(dbremote, {live: true});
        });
    }
}

我检查了所有变量,它们是正确的。
我尝试了纯文本变量。
该脚本确实运行。
我试图以第一次执行的方式向文档中添加信息。
这一切似乎都没有像我在上一个函数中想要的那样向文档添加另一个变量。我认为这与pouchDB的工作方式有关,我不知道。非常感谢您的帮助!

I checked all variables, they are correct. I tried plain text variables. The script does run. I tried to add information to the document the way I did the first time. None of all this seems to add another variable to the document as I wanted in the last function. I think it has to do with the way pouchDB works which I don't know. help is much appreciated!

推荐答案

您的代码中存在许多问题,这些问题会导致PouchDB使用不正确,并可能导致

There are a number of problems in your code that results in bad usage of PouchDB, and may lead to problems.

首先,给您的文档一个与数据库名称相同的ID并没有多大意义。假设您希望每个用户使用一个数据库方法,则可以采用两种方法。

First of all, it does not make a lot of sense to give your document the same id as the name of your database. Assuming you want a one database per user approach, there are two approaches you can follow.

多文档方法
在同一个数据库中用不同的ID制作多个文档。例如,您的同意信息可能像这样存储:

Multiple document approach You can instead make multiple documents within the same database with different id's. For instance, your 'consent' information may be stored like this:

var id = "p" + Date.now() + "-" + Math.floor(Math.random() * 10000);
let dblocal = new PouchDB(id);
document.cookie = id;
let dbremote = 'http://localhost:5984/experiment';
dblocal.put({
    _id: "consent",
    consent: window.document.consent_form.consent_to_share.value
});
dblocal.replicate.to(dbremote, {live: true});

虽然播放列表信息的存储方式如下:

While your playlist information is stored like this:

dblocal.put({
    _id: "playlist",
    name: playlistname,
    itemsChecked: checkedlist
});

单文档方法
第二种选择是存储一个包含与用户相关联的所有要存储信息的文档。在这种方法中,您将希望获取现有文档并在有新信息时对其进行更新。假设您将文档命名为 global-state (即,将第一个代码段中的 consent替换为 global-state),则以下代码将更新文档:

Single-document approach The second option is to store a single document containing all the information you want to store that is associated to a user. In this approach you will want to fetch the existing document and update it when there is new information. Assuming you named your document global-state (i.e. replace "consent" in the first code snippet with "global-state"), the following code will update a document:

dblocal.get("global-state").then((doc)=>{
    doc.loggedIn = true; // or change any other information you want
    return dblocal.put(doc);
}).then((response)=>{
    //handle response
}).catch((err)=>{
    console.log(err);
});

此外,您仅应致电

dblocal.replicate.to(dbremote, {live: true});

函数一次,因为'live'选项指定将来的更改将自动复制到远程数据库。

function once because the 'live' option specifies that future changes will automatically be replicated to the remote database.

这篇关于再次使用相同功能时,不会将记录添加到PouchDB中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆