Fork me on GitHub
#re-frame
<
2021-01-07
>
zendevil.eth04:01:38

I’m getting the following error inexplicably. What does this error mean?

zendevil.eth04:01:49

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}

mikethompson04:01:48

That means you are dispatch-ing a vector where that map is the first element

mikethompson04:01:15

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}])

zendevil.eth05:01:09

Here’s my handler

zendevil.eth05:01:10

(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")))}

   ))

mikethompson05:01:11

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

mikethompson05:01:54

@ps the issue is with the dispatch

zendevil.eth05:01:06

how to fix it?

mikethompson05:01:25

Q: do you understand the problem I described?

zendevil.eth05:01:36

i’m only dispatching things that are handlers

mikethompson05:01:03

As I said, you are dispatching a complicated map

mikethompson05:01:14

(Or at least that seems to me what it happening)

zendevil.eth05:01:24

where am i dispatching it?

zendevil.eth05:01:56

(defn http-get [uri params on-success on-failure]
  {:method :get
   :uri (str "" uri)
   :params params
   :on-success on-success
   :on-failure on-failure
   :response-format (edn/edn-response-format)
   :format (edn/edn-request-format)
   })

zendevil.eth05:01:22

(reg-event-fx
:set-id
 (fn [coeffects [_ response]]
  (js/console.log "response is" response)
   ))

zendevil.eth05:01:31

and the console log in set-id isn’t getting printed

zendevil.eth05:01:45

how to fix this?

mikethompson05:01:08

Perhaps the on-success and on-failure vectors are wrong

zendevil.eth05:01:20

why are they wrong?

mikethompson05:01:23

(defn http-get [uri params on-success on-failure]
  {:method :get
   :uri (str "" uri)
   :params params
   :on-success on-success
   :on-failure on-failure
   :response-format (edn/edn-response-format)
   :format (edn/edn-request-format)
   })

mikethompson05:01:38

Because they should be vectors which are dispatchable

zendevil.eth05:01:48

but they are vectors

zendevil.eth05:01:12

(http-get "/api/sign-in"
               {:google-id google-id}
               [:set-id]
               []
               #_(fn [] (js/console.log "success"))
               #_(fn [] (js/console.log "failure")))

zendevil.eth05:01:24

[:set-id] is the onsuccess vector

zendevil.eth05:01:30

i don’t know how

p-himik08:01:23

@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.

p-himik08:01:12

@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.

mikethompson10:01:21

@p-himik Makes perfect sense!! Good sleuthing.

zendevil.eth10:01:43

I’m trying to persist the app state

zendevil.eth10:01:43

using re-frame-storage

zendevil.eth10:01:43

and I’m using the following:

zendevil.eth10:01:44

(reg-co-fx! :my-app         ;; local storage key
          {:fx :store     ;; re-frame fx ID
             :cofx :store}) ;; re-frame cofx ID

zendevil.eth10:01:44

but this gives the error that the app isn’t registered

zendevil.eth10:01:44

how to fix this error?

mikethompson10:01:51

@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)

👍 3
zendevil.eth10:01:55

when I remove the snippet the error is gone

zendevil.eth10:01:17

yet that’s exactly copied from the github readme of re-frame-storage

zendevil.eth10:01:21

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")))

zendevil.eth10:01:26

How to fix these errors?

p-himik10:01:31

I don't think the error has anything to do with re-frame.

p-himik10:01:08

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.

p-himik10:01:38

Try wrapping new code in try-catch and log the error.

zendevil.eth12:01:41

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

zendevil.eth12:01:10

(reg-event-db
:register-db
 (fn [db [_ params]]
   (go
     (prn "sync storage is "
          (<p! (. AsyncStorage getItem "_id"))))
   app-db
   ))

zendevil.eth12:01:34

I want to take the value in AsyncStrorage getItem and assoc it with the app-db map.

zendevil.eth12:01:38

How to do that?

zendevil.eth12:01:30

is there a workaround for putting this value in the db?

p-himik12:01:54

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.

👍 3
emccue02:01:52

(this is what the sample i gave below does, more or less)