Doesn't add records into PouchDB when used same function over again

╄→гoц情女王★ 提交于 2020-03-03 08:10:31

问题


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});
    }
}

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});
        });
    }
}

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});
        });
    }
}

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!


回答1:


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

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.

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
});

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);
});

Furthermore, you should only call the

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

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



来源:https://stackoverflow.com/questions/59986502/doesnt-add-records-into-pouchdb-when-used-same-function-over-again

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!