Hello, I keep hitting a block with the result type of a promise. Based on the information provided by @ryyppy in the rescript-promise I added a Belt.Result.t type to the response of the mongodb find binding.
// Mongo.res
module Response = {
type t
@send external toArray: t => array<'a> = "toArray"
}
module Collection = {
type t
@send
external find: (t, 'query) => Promise.t<Belt.Result.t<Response.t, Js.Nullable.t<Js.Exn.t>>> =
"find"
}
module Db = {
type t
type collectionName = string
@send external collection: (t, collectionName) => Collection.t = "collection"
}
module ClientInstance = {
type t
type dbName = string
@send external db: (t, dbName) => Db.t = "db"
}
module Client = {
type t
@module("mongodb") @new external make: string => t = "MongoClient"
@send external connect: t => Promise.t<ClientInstance.t> = "connect"
@send external close: t => unit = "close"
}
and the corresponding
let uri = "mongodb://localhost:27017"
let dbName = "rescript"
let collectionName = "documents"
let query = {"isLearningRescript": true}
open Mongo
open Promise
let client = Client.make(uri)
client
->Client.connect
->then(instance => {
instance->ClientInstance.db(dbName)->Db.collection(collectionName)->resolve
})
->thenResolve(collection => {
>> collection->Collection.find(query)->Response.toArray->Ok
})
->catch(e => {
switch e {
| JsError(obj) =>
switch Js.Exn.message(obj) {
| Some(msg) => Error(msg)
| None => Error("Unknown JS error")
}
| _ => Error("A non JS error")
}->resolve
})
->finally(() => {
client.Client.close
})
->ignore
I am returning a Belt.Result.t for both my catch and previous then. So what am I doing wrong? Help will be highly appreciated.