简体   繁体   中英

mongoose listCollections() not returning collections name instead return a promise pending

I want to create model on the fly so I am using below code for achieving same.

var connection = mongoose.createConnection(metadb_uri, mongo_opts, function() {
   log.debug(process.env.DB_ENV+' setup done')
});

const _schema = new Schema({
   count:Number,
   users:{type: mongoose.Schema.Types.Mixed, default: {}} 
});

let name = 'name_123';

//trying to get collection names
connection.db.listCollections.toArray().then((names) => {

    if (!(name in names.name)) {
       let userAnalyticsDB = connection.model(name,_schema);
       module.exports.Collection = userAnalyticsDB;
    }
  })
  .catch((err)=>{
    return err;
  })
});

so connection.db.listCollections.toArray() returns promise state as pending. Can anyone have an idea , why it is giving promise value as undefined .

Please give me some hint.

Thanks for your answers and help. I debug the issue and found that it was related to Promise hell. I wrote promise within promise. so it was returning promise as pending and undefined

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