Fork me on GitHub
#luminus
<
2019-01-21
>
Bravi15:01:46

hi everyone

Bravi15:01:55

any ideas what does this mean? mount.core.DerefableState cannot be cast to com.mongodb.DB

Bravi15:01:25

I create a project using lein new luminus my-app +re-frame +mongodb

Bravi15:01:37

which generates my_app.db.core file, that has the following function

(defn create-user [user]
  (mc/insert db "users" user))

Bravi15:01:08

and if I call this function from my routes/home.clj file, I get that error basically

manutter5115:01:48

That means you’re using mount to manage your connection to the database, and it hasn’t connected to the database yet.

Bravi15:01:04

could it be a credentials error?

Bravi15:01:11

or is it just taking time?

manutter5115:01:46

Could be either one. I’d try playing with it at the repl and see if it’s throwing any errors and/or returning a value for db eventually.

manutter5115:01:04

Also be sure mount/start has finished before running the create-user fn.

Bravi15:01:34

is there a way to make sure it has finished ? 😄

manutter5115:01:17

Actually, that’s probably a bad way to phrase it. I should have said make sure your code is calling mount/start before the call to create-user — if they’re both in the same thread, then you should be good

Bravi15:01:59

ah right. yeah I think they are, so probably it’s something to do with credentials.. I’ll see if I can connect to repl somehow

Bravi15:01:31

throws the same exact error in repl 😞

manutter5115:01:04

I’m using your lein new command to create my own copy of my-app and see if I can see anything useful — have you made many change to the project yet, other than the mongo credentials?

Bravi15:01:50

nope, nothing at all

manutter5115:01:04

ok, good, I should be seeing the same code you are then

Bravi15:01:31

I’ve just created a new function under db/core.clj

(defn get-users []
  (mc/find-maps db "users"))

Bravi15:01:38

and it throws the same error

manutter5115:01:31

What’s your working environment? Emacs? Atom? Cursive?

Bravi15:01:39

well, spacemacs

Bravi15:01:01

jack-in doesn’t work unfortunately

manutter5115:01:02

Ok cool. My spacemacs is getting a bit rusty, but I can suggest some things

Bravi15:01:08

yeah sure

manutter5115:01:10

Try shutting everything down and then do

$ lein repl
> (in-ns 'my-app.core)
> (start-app)

manutter5115:01:32

That should give you some output about mount starting things up

Bravi15:01:35

👍 I’ll give it a shot now

manutter5115:01:16

then maybe (clojure.pprint/pprint my-app.db.core/db) and see if you have a db connection or a mount/DerefableState

Bravi15:01:25

it is a DB connection

manutter5115:01:08

Ok, so it sounds like start-app isn’t getting called. How are you starting the app?

Bravi15:01:41

lein run and then lein figwheel

Bravi15:01:00

and I haven’t changed any defaults

manutter5115:01:40

Hmm, ok, lein run should be calling start-app — do you have an environment variable named DATABASE_URL?

Bravi15:01:23

ok I think it works now

Bravi15:01:42

I’ll just restart the app once more and test

Bravi15:01:49

re: your question, I don’t have a variable with that name. but I think for some odd reason, it works now..

manutter5115:01:34

Maybe it just needed a bit of extra time to start up before you call lein figwheel

manutter5115:01:49

Here’s what I get when I start it up:

env DATABASE_URL="" lein run
2019-01-21 10:47:04,271 [main] DEBUG org.jboss.logging - Logging Provider: org.jboss.logging.Slf4jLoggerProvider
2019-01-21 10:47:04,643 [main] INFO  my-app.env -
-=[my-app started successfully using the development profile]=-
2019-01-21 10:47:04,768 [main] INFO  luminus.http-server - starting HTTP server on port 3000
2019-01-21 10:47:04,814 [main] DEBUG io.undertow - starting undertow server [email protected]
2019-01-21 10:47:04,824 [main] INFO  org.xnio - XNIO version 3.3.6.Final
2019-01-21 10:47:04,932 [main] DEBUG io.undertow - Configuring listener with protocol HTTP for interface 0.0.0.0 and port 3000
2019-01-21 10:47:04,986 [main] INFO  org.projectodd.wunderboss.web.Web - Registered web context /
2019-01-21 10:47:04,988 [main] INFO  my-app.nrepl - starting nREPL server on port 7000
2019-01-21 10:47:05,007 [main] INFO  my-app.core - #'my-app.config/env started
2019-01-21 10:47:05,007 [main] INFO  my-app.core - #'my-app.handler/init-app started
2019-01-21 10:47:05,007 [main] INFO  my-app.core - #'my-app.handler/app started
2019-01-21 10:47:05,007 [main] INFO  my-app.core - #'my-app.core/http-server started
2019-01-21 10:47:05,007 [main] INFO  my-app.core - #'my-app.core/repl-server started

Bravi15:01:09

yep, the same here. I wasn’t getting that before though

Bravi15:01:15

so yeah it could be that it was taking time

Bravi15:01:29

thank you very much 🙂

Bravi15:01:37

I appreciate your help

manutter5115:01:44

Ok, cool, I’d say do lein run, wait for that stuff to print, then lein figwheel, should be reliable.

temco15:01:03

how did you call the fn inside db/core.clj from routes/home.clj?

Bravi15:01:21

I required it like so

[my-app.db.core :as db]

Bravi15:01:59

and then

(defn home-page []
  (let [users (db/get-all-users)]
    (layout/render "home.html" {:users users})))

Bravi15:01:27

and in my home.html I was doing

{{ users }} and {{ users|count }}

temco15:01:19

what's the original code generated by the template like?

Bravi15:01:52

original one just didn’t have that function, but even if I used the one in the original:

(defn get-user [id]
  (mc/find-one-as-map db "users" {:_id id}))
it still didn’t work

Bravi15:01:58

it was still throwing the same error

temco16:01:58

in fact, when you called the db.core/some_fn in routes.home ns, the error was thrown

temco16:01:43

without the calling, even if you add some fns into db.core ns, no error will be thrown

temco16:01:57

I think some_fn of db.core ns must have be called before the mount/start fn family called

Bravi16:01:07

maybe, but then i’m not sure how that would happen, as routes.home is started from start-app function and only then should it have called the db.core function

Bravi16:01:57

ok another question.. loads of frameworks like Laravel (PHP) or Buffalo (Golang) come with tools like artisan and grifts respectively, that run command line commands. for example php artisan db:seed will seed the database and then you can add as many command line functions as you want. so I want to add something similar to my luminus app. but I can’t figure out how would I do it, since lein run will always call the main function. Is there a way to somehow map some custom functions to lein scripts / commands? like for example if I run lein seed to call my-app.db.seeds/database-seeder function?

temco16:01:12

that's plugin

temco16:01:44

if you use a plugin lein-seed, you could use the command lein seed