0

I have a database connection for the Ojai client in node-js where i have a lot of promises, that needs to be resolved in order to establish the connection


const getAllStatuses = async (connectionString, tablename) =>{
   let connection;
   return ConnectionManager.getConnection(connectionString)
    .then(conn =>{
        connection = conn;
        return connection.getStore(tablename)
    }).then(newStore =>{
        const store = newStore;
        return store.find({})
    }).then(queryResult =>{
        queryResult.on('data', (document =>{
        return document
        }))
    }).finally(()=>{
        connection.close()
    })
}

right now i have this in a file called databaseMapper.js

I can't seem to figure out how to return a value from this promise chainging, because i get undefined in my controller.

module.exports.getAll = async () =>{
    const data = await databaseMapper.getAllStatuses(connectionString, tables.status)
    console.log(data)
    return data
}

I have tried to make the function async and then await the promise, but stil with no luck.

Can anybody help me achieve me return the correct value (i have tested with console log, that a value is actually returned)

Kristoffer Tølbøll
  • 3,157
  • 5
  • 34
  • 69
  • Your `queryResult.on('data', (document => {` is callback-based, Putting it inside a `.then` doesn't automatically connect it to the Promise chain outside - instead, construct a Promise that resolves when the `.on` fires – CertainPerformance Mar 21 '20 at 10:19
  • The problem is that your last fulfillment handler (the one calling `on`) has no `return` in it. There's a `return` in the callback you're passing `on`, but not in the fulfillment handler you're passing `then`. So that implicitly returns `undefined`, which becomes the fulfillment value of the promise chain. You'll need to wrap that `on` call in a promise to use it with a promise chain. (Also, since `getAllStatuses` is an `async` function, you can use `await` rather than `.then`, and `try`/`finally` rather than `.finally`.) – T.J. Crowder Mar 21 '20 at 10:20
  • Also note that your code doesn't quite handle `getConnection` rejections correctly; your `finally` handler tries to call `close` on `undefined` in that case. The error that generates is suppressed, though, since there's already an error in progress and it's in a `finally` handler. Separately, given how much of the API you're using is already promise-based, I suspect there's a promise-based alternative to that `on` callback API. But if not, it's simple enough to write a wrapper function, something along these lines: https://pastebin.com/EZZ913Ej – T.J. Crowder Mar 21 '20 at 10:24
  • @T.J.Crowder Perfect! thank you. If you post as answer i will accept! – Kristoffer Tølbøll Mar 21 '20 at 10:31
  • 1
    @baileyhaldwin - The question is closed, so it's not possible to post an answer. :-) But I figured the comment would be helpful. Happy coding! – T.J. Crowder Mar 21 '20 at 10:53
  • @T.J.Crowder is it possible to do this, if it returns multiple `document` s? – Kristoffer Tølbøll Mar 24 '20 at 14:10
  • @baileyhaldwin - I assume so, I don't know ojai. If there can be multiple `data` events, I'm guessing you could keep them in an array (or similar) and then resolve the project in response to an `end` event or something. – T.J. Crowder Mar 24 '20 at 14:54
  • yes exactly what i figured.. But for the `queryResult.on()` is it possible to map these? – Kristoffer Tølbøll Mar 24 '20 at 14:55
  • i tried something like this `function queryResultPromise(queryResult) { return new Promise((resolve, reject) => { let data = [] queryResult.on("data", doc => data.push(doc)); queryResult.on("end", (resolve(data))) // ...presumably something here to hook an error event and call `reject`... }); }` but it returned an empty array – Kristoffer Tølbøll Mar 24 '20 at 14:58

0 Answers0