You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@openwhisk.apache.org by GitBox <gi...@apache.org> on 2018/02/07 14:09:33 UTC

[GitHub] rabbah closed pull request #155: #112 Adding feature to insert document when overwrite and putIfAbsent?

rabbah closed pull request #155: #112 Adding feature to insert document when overwrite and putIfAbsent?
URL: https://github.com/apache/incubator-openwhisk-package-cloudant/pull/155
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/actions/database-actions/write-document.js b/actions/database-actions/write-document.js
index b96b6af..2db401c 100755
--- a/actions/database-actions/write-document.js
+++ b/actions/database-actions/write-document.js
@@ -45,7 +45,7 @@ function main(message) {
 
 /**
  * If id defined and overwrite is true, checks if doc exists to retrieve version
- * before insert. Else inserts.
+ * before insert. Else inserts a new doc.
  */
 function insertOrUpdate(cloudantDb, overwrite, doc) {
     if (doc._id) {
@@ -62,8 +62,19 @@ function insertOrUpdate(cloudantDb, overwrite, doc) {
                                reject(err);
                             });
                     } else {
-                        console.error('error', error);
-                        reject(error);
+                        if(error.statusCode === 404) {
+                            // If document not found, insert it
+                            insert(cloudantDb, doc)
+                                .then(function (response) {
+                                    resolve(response);
+                                })
+                                .catch(function (err) {
+                                    reject(err);
+                                });
+                        } else {
+                            console.error('error', error);
+                            reject(error);
+                        }
                     }
                 });
             });


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services