hi everyone
any ideas what does this mean?
mount.core.DerefableState cannot be cast to com.mongodb.DB
I create a project using lein new luminus my-app +re-frame +mongodb
which generates my_app.db.core
file, that has the following function
(defn create-user [user]
(mc/insert db "users" user))
and if I call this function from my routes/home.clj
file, I get that error basically
That means you’re using mount
to manage your connection to the database, and it hasn’t connected to the database yet.
could it be a credentials error?
or is it just taking time?
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.
Also be sure mount/start
has finished before running the create-user
fn.
is there a way to make sure it has finished ? 😄
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
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
throws the same exact error in repl 😞
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?
nope, nothing at all
ok, good, I should be seeing the same code you are then
I’ve just created a new function under db/core.clj
(defn get-users []
(mc/find-maps db "users"))
and it throws the same error
What’s your working environment? Emacs? Atom? Cursive?
emacs
well, spacemacs
jack-in
doesn’t work unfortunately
Ok cool. My spacemacs is getting a bit rusty, but I can suggest some things
yeah sure
Try shutting everything down and then do
$ lein repl
> (in-ns 'my-app.core)
> (start-app)
That should give you some output about mount starting things up
👍 I’ll give it a shot now
then maybe (clojure.pprint/pprint my-app.db.core/db)
and see if you have a db connection or a mount/DerefableState
it is a DB connection
Ok, so it sounds like start-app
isn’t getting called. How are you starting the app?
lein run
and then lein figwheel
and I haven’t changed any defaults
Hmm, ok, lein run
should be calling start-app
— do you have an environment variable named DATABASE_URL
?
ok I think it works now
I’ll just restart the app once more and test
re: your question, I don’t have a variable with that name. but I think for some odd reason, it works now..
Maybe it just needed a bit of extra time to start up before you call lein figwheel
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
yep, the same here. I wasn’t getting that before though
so yeah it could be that it was taking time
thank you very much 🙂
I appreciate your help
Ok, cool, I’d say do lein run
, wait for that stuff to print, then lein figwheel
, should be reliable.
:thumbsup:
how did you call the fn inside db/core.clj
from routes/home.clj
?
I required it like so
[my-app.db.core :as db]
and then
(defn home-page []
(let [users (db/get-all-users)]
(layout/render "home.html" {:users users})))
and in my home.html
I was doing
{{ users }} and {{ users|count }}
what's the original code generated by the template like?
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 workit was still throwing the same error
in fact, when you called the db.core/some_fn
in routes.home
ns, the error was thrown
without the calling, even if you add some fns into db.core
ns, no error will be thrown
I think some_fn of db.core
ns must have be called before the mount/start
fn family called
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
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?
that's plugin
if you use a plugin lein-seed, you could use the command lein seed