luminus

Bravi 2019-01-21T15:20:46.023Z

hi everyone

Bravi 2019-01-21T15:20:55.023300Z

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

Bravi 2019-01-21T15:21:25.024Z

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

Bravi 2019-01-21T15:22:37.024700Z

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

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

Bravi 2019-01-21T15:23:08.025300Z

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

2019-01-21T15:24:48.025900Z

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

Bravi 2019-01-21T15:25:04.026200Z

could it be a credentials error?

Bravi 2019-01-21T15:25:11.026500Z

or is it just taking time?

2019-01-21T15:25:46.027200Z

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.

2019-01-21T15:27:04.027700Z

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

Bravi 2019-01-21T15:27:34.028100Z

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

2019-01-21T15:29:17.029500Z

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

Bravi 2019-01-21T15:29:59.030200Z

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

Bravi 2019-01-21T15:32:31.030500Z

throws the same exact error in repl 😞

2019-01-21T15:36:04.031400Z

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?

Bravi 2019-01-21T15:36:50.031600Z

nope, nothing at all

2019-01-21T15:37:04.032200Z

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

Bravi 2019-01-21T15:37:31.032700Z

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

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

Bravi 2019-01-21T15:37:38.033Z

and it throws the same error

2019-01-21T15:38:31.034100Z

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

Bravi 2019-01-21T15:38:37.034300Z

emacs

Bravi 2019-01-21T15:38:39.034500Z

well, spacemacs

Bravi 2019-01-21T15:39:01.035100Z

jack-in doesn’t work unfortunately

2019-01-21T15:39:02.035200Z

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

Bravi 2019-01-21T15:39:08.035600Z

yeah sure

2019-01-21T15:40:10.036600Z

Try shutting everything down and then do

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

2019-01-21T15:40:32.037100Z

That should give you some output about mount starting things up

Bravi 2019-01-21T15:41:35.037700Z

👍 I’ll give it a shot now

2019-01-21T15:42:16.038400Z

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

Bravi 2019-01-21T15:43:25.038700Z

it is a DB connection

2019-01-21T15:44:08.039200Z

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

Bravi 2019-01-21T15:44:41.039500Z

lein run and then lein figwheel

Bravi 2019-01-21T15:45:00.039900Z

and I haven’t changed any defaults

2019-01-21T15:45:40.040600Z

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

Bravi 2019-01-21T15:46:23.040900Z

ok I think it works now

Bravi 2019-01-21T15:46:42.041300Z

I’ll just restart the app once more and test

Bravi 2019-01-21T15:47:49.042100Z

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

2019-01-21T15:48:34.042700Z

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

2019-01-21T15:48:49.043100Z

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

env DATABASE_URL="<mongodb://localhost/my-app>" 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 io.undertow.Undertow@2223232e
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

Bravi 2019-01-21T15:49:09.043600Z

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

Bravi 2019-01-21T15:49:15.043800Z

so yeah it could be that it was taking time

Bravi 2019-01-21T15:49:29.044300Z

thank you very much 🙂

Bravi 2019-01-21T15:49:37.044700Z

I appreciate your help

2019-01-21T15:49:44.044900Z

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

2019-01-21T15:49:49.045100Z

:thumbsup:

temco 2019-01-21T15:55:03.045700Z

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

Bravi 2019-01-21T15:56:21.046Z

I required it like so

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

Bravi 2019-01-21T15:56:59.046700Z

and then

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

Bravi 2019-01-21T15:57:27.047300Z

and in my home.html I was doing

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

temco 2019-01-21T15:58:19.047800Z

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

Bravi 2019-01-21T15:59:52.048400Z

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

Bravi 2019-01-21T15:59:58.048700Z

it was still throwing the same error

temco 2019-01-21T16:02:58.049900Z

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

temco 2019-01-21T16:04:43.051100Z

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

temco 2019-01-21T16:05:57.052Z

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

Bravi 2019-01-21T16:20:07.053200Z

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

Bravi 2019-01-21T16:23:57.057100Z

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?

temco 2019-01-21T16:25:12.057400Z

that's plugin

temco 2019-01-21T16:25:44.058Z

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