Collection's bulkWrite method fails when it used as a promise

XMLWordPrintableJSON

    • Type: Bug
    • Resolution: Done
    • Priority: Major - P3
    • 2.0.40
    • Affects Version/s: 2.0.37
    • Component/s: None
    • None
    • None
    • None
    • None
    • None
    • None
    • None

      The following example shows that the methods works with the callback, but if you use it as a promise (Commented code) it freezes or throws an exception depending on the Promise implementation.

      *jslint node:true*/
      'use strict';
      var Client = require("mongodb").MongoClient,
          db;
      
      Client.connect("mongodb://localhost/test").then(function (conn) {
          db = conn;
          var bwt = conn.collection("bwt");
          //return bwt.bulkWrite([{ insertOne: { document: { a: 1 } } }]);
          return new Promise(function (resolve, reject) {
              bwt.bulkWrite([{ insertOne: { document: { a: 1 } } }], function (err, r) {
                  if (err) {
                      reject(err);
                  } else {
                      resolve(r);
                  }
              });
          });
      }).then(function (r) {
          console.log(r.insertedCount); //1
      }).catch(function (reason) {
          console.error(reason);
      }).then(function () {
          if (db) {
              db.close();
          }
      });
      
      

              Assignee:
              Christian Amor Kvalheim
              Reporter:
              VĂ­ctor Herraiz Posada
              None
              Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

                Created:
                Updated:
                Resolved: