简体   繁体   中英

How do I update a doc in Cloudant using Cloudant Node.js

So, what I'm doing should be really simple, and maybe it is and I'm just doing something wrong. I want to update an existing document in my database but I'm having some issues, can someone please advise?

Nano's Documentation states the following for insert:

db.insert(doc, [params], [callback])

Therefore, I should surely be able to do the following:

 var user = {
   'firstname' : 'my',
   'secondname' : 'name'
};

db.insert(user, {_rev: '2-cc5825485a9b2f66d79b8a849e162g2f'}, function(err, body) {}); 

However, whenever I try this, it creates an entirely new document. If I do the following then it will indeed update my document, but of course, with nothing in this document other than the _rev:

db.insert({_rev: '2-cc5825485a9b2f66d79b8a849e162g2f'}, function(err, body) {});

So the question is, how do I pass in my object and get it to update, rather than creating a new record?

var user = {
   'firstname' : 'my',
   'secondname' : 'name',
   '_id': <id from prev object>,
   '_rev': <rev from prev object>
};

db.insert(user, function(err, body) {}); 

the _id and _rev are both required in order for the update to work. they should be in the object that you are sending also.

The first argument in the db.insert(...) command is the document which you want to create/update. If you pass in a doc with a ._rev attribute, then it will replace the document with that same _rev in Cloudant with the doc passed in as the first argument of your db.insert(...) . If the doc does not include a ._rev attribute, then Cloudant will create an entirely new document.

This explains the behavior you were experiencing in both the scenarios you tried. In order to make an update to your doc, make sure to include ._id and ._rev attributes, along with the rest of your doc's attributes when you use it as the first argument to your db.insert(...) function.

Got it! Here's the solution:

db.get('user', { revs_info: true }, function(err, doc) {

   if (!err) {
     console.log(doc);

     doc.firstname = 'my';
     doc.secondname = 'name';

     db.insert(doc, doc.id, function(err, doc) {
        if(err) {
           console.log('Error inserting data\n'+err);
           return 500;
        }
        return 200;
     });
   }
});

First get the record id and rev id (_id,_rev)

const newData={email:"aftabfalak956@gmail.com",name:"Aftab Falak"}

cloudant.use("user").find({selector:{_id:"user_id"}}, (err, documents) => {

    var revision = documents.docs[0]._rev;
    const data={...documents.docs[0],...newData};
    
    cloudant.use("user").insert(data,{_rev:revision},function(err){
        if (!err) {
            console.log('success', 'The record was updated successfully');
        }
        else {
            console.log('failure', err);
        }
    });

});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM