javascriptmysqlnode.jsdatabasemysql-x-devapi

mysql-xdevapi getCollections promise return value


I've been trying to output the array of collections in my database schema. Assuming my session works, I'd get an Promise{} output below which I think is normal.

        .then(()=>{
            return sess.getSchema('mySchema').getCollection('myCollection')
            .find().fields(['name','age'])
            .execute(row=>{
                console.log(row)
            })
        })
        .then(()=>{
            let r = sess.getSchema('mySchema').getCollections()
            console.log(r)
            return r
        })

but if I tried to get the values in the promise

            let r = sess.getSchema('mySchema').getCollections()
            r.then(v=>{
                console.log(v)
            })

It returns me these session callback functions

 [
  {
    getSession: [Function: getSession],
    add: [Function: add],
    addOrReplaceOne: [Function: addOrReplaceOne],
    count: [Function: count],
    existsInDatabase: [Function: existsInDatabase],
    find: [Function: find],
    getName: [Function: getName],
    getSchema: [Function: getSchema],
    inspect: [Function: inspect],
    modify: [Function: modify],
    remove: [Function: remove],
    removeOne: [Function: removeOne],
    replaceOne: [Function: replaceOne],
    dropIndex: [Function: dropIndex],
    createIndex: [Function: createIndex],
    getOne: [Function: getOne]
  }
]

Solution

  • That's just the way the API works. The getCollections() method returns an array of Collection instances. Each instance has that specific set of methods.

    So, for instance, if you want to get the collection names, you can do:

    sess.getSchema('mySchema').getCollections()
      .then(collections => {
        console.log(collections.map(c => c.getName()))
      })
    

    Disclaimer: I'm the lead developer of the MySQL X DevAPI Connector for Node.js