I’m getting the following error inexplicably. What does this error mean?
re-frame: no :event handler registered for: {"__hash": null, "arr": [{"_hash": null, "cljs$lang$protocol_mask$partition0$": 2153775105, "cljs$lang$protocol_mask$partition1$": 4096, "fqn": "_id", "name": "_id", "ns": null}, "5ff6924434880e93f086fc14"], "cljs$lang$protocol_mask$partition0$": 16647951, "cljs$lang$protocol_mask$partition1$": 139268, "cnt": 1, "meta": null}
That means you are dispatch
-ing a vector where that map is the first element
Ie. it is as if your are doing:
(dispatch [{"__hash": null, "arr": [{"_hash": null, "cljs$lang$protocol_mask$partition0$": 2153775105, "cljs$lang$protocol_mask$partition1$": 4096, "fqn": "_id", "name": "_id", "ns": null}, "5ff6924434880e93f086fc14"], "cljs$lang$protocol_mask$partition0$": 16647951, "cljs$lang$protocol_mask$partition1$": 139268, "cnt": 1, "meta": null}])
Here’s my handler
(reg-event-fx
:sign-in
(fn [coeffects [_ google-id]]
(js/console.log "google id is!" google-id)
{:db
(assoc (:db coeffects) :signed-in true)
:http-xhrio
(http-get "/api/sign-in"
{:google-id google-id}
[:set-id]
[]
#_(fn [] (js/console.log "success"))
#_(fn [] (js/console.log "failure")))}
))
re-frame is extracting the first element of the event vector ... which is that map ... and then trying to find an event handler which is registered for it
@ps the issue is with the dispatch
how to fix it?
Q: do you understand the problem I described?
i’m only dispatching things that are handlers
Clearly not
As I said, you are dispatching a complicated map
(Or at least that seems to me what it happening)
where am i dispatching it?
(defn http-get [uri params on-success on-failure]
{:method :get
:uri (str "<http://localhost:3000>" uri)
:params params
:on-success on-success
:on-failure on-failure
:response-format (edn/edn-response-format)
:format (edn/edn-request-format)
})
(reg-event-fx
:set-id
(fn [coeffects [_ response]]
(js/console.log "response is" response)
))
and the console log in set-id isn’t getting printed
how to fix this?
Perhaps the on-success
and on-failure
vectors are wrong
why are they wrong?
(defn http-get [uri params on-success on-failure]
{:method :get
:uri (str "<http://localhost:3000>" uri)
:params params
:on-success on-success
:on-failure on-failure
:response-format (edn/edn-response-format)
:format (edn/edn-request-format)
})
Because they should be vectors which are dispatchable
but they are vectors
(http-get "/api/sign-in"
{:google-id google-id}
[:set-id]
[]
#_(fn [] (js/console.log "success"))
#_(fn [] (js/console.log "failure")))
[:set-id] is the onsuccess vector
now it works
i don’t know how
@mikethompson I don't think you were entirely correct. That "complicated map" is just how a keyword within a 1-entry map is printed out by a pure text JS console:
cljs.user=> (js/console.log {:x 1})
{
meta: null,
cnt: 1,
arr: [
{
ns: null,
name: 'x',
fqn: 'x',
_hash: 2099068185,
'cljs$lang$protocol_mask$partition0$': 2153775105,
'cljs$lang$protocol_mask$partition1$': 4096
},
1
],
__hash: null,
'cljs$lang$protocol_mask$partition0$': 16647951,
'cljs$lang$protocol_mask$partition1$': 139268
}
nil
Although, I have no idea how to get that exact message in a browser's console - at least Chrome and Firefox wrap it in an interactive object that looks a bit differently.
But of course, something was not right in the original code/state of the app in the first place.@ps I'm pretty sure I know what happened.
1. You set :on-failure
to []
and make the request to /api/sign-in
2. The request to fails
3. The body of the failure response is parsed and conj
-ed into that empty vector (seems like that body contained a map {:_id "5ff6924434880e93f086fc14"}
)
4. That vector then passed to dispatch
In short, don't use empty vectors when specifying :on-success
and :on-failure
. Specify nil
instead.
@p-himik Makes perfect sense!! Good sleuthing.
I’m trying to persist the app state
using re-frame-storage
and I’m using the following:
(reg-co-fx! :my-app ;; local storage key
{:fx :store ;; re-frame fx ID
:cofx :store}) ;; re-frame cofx ID
but this gives the error that the app isn’t registered
how to fix this error?
@p-himik I don't think that :on-failure nil
would work. Best to just not include the key :on-failure
(rather than to have it with a value of nil
)
when I remove the snippet the error is gone
yet that’s exactly copied from the github readme of re-frame-storage
i get the same error as well when using the following:
(defn my-reg-event-db
[event-id handler]
(reg-event-fx
event-id
[(persist-db :my-app :persistent)]
(fn [{:keys [db]} event-vec]
{:db (handler db event-vec)})))
;; ...
(my-reg-event-db
:read-foo-store-bar
(fn [db _]
(print (get-in db [:persistent :foo]))
(assoc-in db [:persistent :bar] "qux")))
How to fix these errors?
I don't think the error has anything to do with re-frame.
Although if the "A module failed to load due to an error" clause is what's happening, then it can be related to re-frame. But there's no way to figure it out without the underlying error.
Try wrapping new code in try-catch and log the error.
I’ve successfully stored a value with AsyncStorage, but I want to get the value and use the value. But I can’t pass the value because the function returns a promise, which must be wrapped in a go block
(reg-event-db
:register-db
(fn [db [_ params]]
(go
(prn "sync storage is "
(<p! (. AsyncStorage getItem "_id"))))
app-db
))
I want to take the value in AsyncStrorage getItem and assoc it with the app-db map.
How to do that?
is there a workaround for putting this value in the db?
Use reg-event-fx
instead, create another event and dispatch
it from the promise's resolve
callback.
Ideally, wrap it all up in an effect to keep that event handler pure.