ES6 promise不会始终适用于mongodb复制集

ES6 promise不会始终适用于mongodb复制集,mongodb,ecmascript-6,promise,Mongodb,Ecmascript 6,Promise,我确实跟着。答案4 by(),适用于常规mongodb服务器。但它并不总是适用于mongoDB复制集 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 u

我确实跟着。答案4 by(),适用于常规mongodb服务器。但它并不总是适用于mongoDB复制集

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

但它并不总是有效的
-您是否可以将您的问题扩展到包含相关代码,也许更重要的是,描述代码失败的方式(错误日志等)。。。因为
它将不工作
根本无法描述问题。后来我发现我的网络设置错误。上述代码在本地或通过正确设置的VPN工作。同样,ECMA6 mongo驱动程序也可以与replicationSet一起工作。请放弃这个问题!!