0

I did follow How to use MongoDB with promises in Node.js?. The answer 4 by(https://stackoverflow.com/users/5371505/pirateapp), works well with regular mongodb server. But it will not work always with a mongoDB replication set.

const mongodb = require('mongodb');
const MongoClient = mongodb.MongoClient;

// the url talking to replicaSet does not work, while the url with regular mongoDB sever seems working for me.
// const url = 'mongodb://alexlai:alex1765@arch16GMongo01.yushei.me:27017,arch16GMongo02.yushei.me:27017,arch16GMongo03:27017/YuShei?replicaSet=odroid00&connectTimeoutMS=300000';
 url = 'mongodb://172.16.1.108/YuShei';

let db = {
    open : open,
}

function open(){
  return new Promise((resolve, reject)=>{
    MongoClient.connect(url, (err, db) => {
      if (err) {
        reject(err);
      } else {
        resolve(db);
      }
    });
  });
}

function close(db){
    if(db){
        db.close();
    }
}

// module.exports = db;

// const db = require('./mongoDBServer.js');

const assert = require('assert');
const collectionName= 'yuTsaiLpr20161021'; // a collection contains 500 docs.

// this will hold the final array taht will be sent to browser
// a global variable will be declared with upper camel
let Array = [];
// this will hold database object for latter use
let Database = '';
// global query string and projection
let Query = {};
let Projection = {};
let Collection ={};

let checkoutCarPromise = new Promise((resolve, reject)=>{
  Database = null;
  db.open() // no ';' semi-column this is a promise, when successful open will be reolved and return with db object, or reject
  .then((db)=>{
    Database = db; // save it globally
    return db.collection(collectionName);
  })
  .then((collection)=>{
    if(collection == 'undefined') reject('collection not found!!');
    Collection = collection; //seave it globally
    return(collection);
  })
  .then((collection)=>{
    return collection.find(); // return a cursor
  })
  .then((cursor)=>{
    return cursor.toArray();
  })
  .then((array)=>{
    console.log('array[499]: ', array[499]);
    Array.push(array[499]);
  })
  .then(()=>{ // reread to find this car
    return Collection.find({plateText:{$regex: /8920/}});
  })
  .then((cursor)=>{
    return cursor.toArray();
  })
  .then((array)=>{
    Array.push(array);
    resolve(Array);
  })
})
.catch((err)=>{
  return(err);
  console.error('the checkoutCarPromiserror is: ', err);
})

Promise.all([checkoutCarPromise]).then(results => {
  console.log('checkoutCarPromise last resolve value: ', results[0]);
  console.log('Array: ', Array);
  Database.close();
})

// this will get you more infos about unhandled process
process.on("unhandledRejection", (reason) => {
  console.log(reason)
})
Community
  • 1
  • 1
Alex Lai
  • 11
  • 1
  • 2
  • `But it will not work always` - could you perhaps expand your question to include relevant code, and probably more important, describe in what way the code fails (error logs, etc) ... because `it will not work` does not describe the problem at all – Jaromanda X Jan 15 '17 at 02:42
  • Latter I found out that my network set up was wrong. The above codes work locally or through a properly set up VPN. Again ECMA6 mongo drivers do work with replicationSet. Please discard this question!! – Alex Lai Feb 14 '17 at 03:06

0 Answers0