Clojure LuminusWebフレームワークでのMongoDBの構成

CodeKingPlusPlus

Luminusプロジェクト内でMongoDBデータベースを構成する際に問題が発生します。これは、leinテンプレートを考えると非常に簡単ですhttps//github.com/yogthos/luminus-template入力lein new luminus <name> +mongodbすると、ファイルであるデフォルトのmongoDBセットアップが表示されます。src/app-name/db/core.cljサーバーを実行するには、「which」と入力しlein ring serverてWebブラウザーを開き、localhost:3000デフォルトでそれを指すようにします。

デフォルトのホームページが表示され、「MongoDBの設定が必要です」と表示されます。同じファイルで構成できることがわかります。src/app-name/db/core.clj.さまざまなことを試しましたが、現在試していることと、私にとって最も意味のあることは次のとおりです。

(defonce coll "collection-name")
(defonce db (let [uri "mongodb://127.0.0.1/db-name"
                  {:keys [conn db]} (mg/connect-via-uri uri)]
              db))

残念ながら、ブラウザを接続しても、同じ「MongoDB構成が必要です」というメッセージが表示されます。また、アプリケーションで定義されているCURLおよびさまざまなHTTPルートを使用して、データベースにアクセスしようとしましたが、成功しませんでした。しかし奇妙なのは、これがREPLで機能することです。

編集:より明確にするために、ここにREPLの例があります:

clj-project-name.db.core> (get-replies 2)
["mew-mew" [1.0 "hello"]]

コードには次の部分があります。

 (ns clj-project-name.routes.home
  (:require [compojure.core :refer :all]
            [clj-project-name.layout :as layout]
            [clj-project-name.util :as util]
            [clj-project-name.db.core :as project-db]))

(defn get-replies [id] (mc/distinct db coll "replies" {:_id id}))
(GET "/user" [id] (user-page id))  ; defined in home-routes inside namespace clj-project-name.routes.home 
(defn user-page [& [id]]           ;defined inside namespace clj-project-name.routes.home
  (layout/render "user.html"
                 {:id id
                  :replies (projectl-db/get-replies id)}))

<h1>User {{id}}'s page</h1>           ; part of the HTML template
<p> <b>Replies:</b> {{replies}} </p>  

ブラウザに読み込まれるページは次のとおりです。

mongodbへの呼び出しが機能しないことを示すページ

As we can see, the replies list is empty, when it should be ["mew-mew" [1.0 "hello"]] as we saw in the REPL.

EDIT: Another oddity is that just when the browser is loaded after typing lein ring server I can see the following output from mongodb in the terminal:

2014-12-02T21:16:57.941-0500 [initandlisten] connection accepted from 127.0.0.1:38854 #28 (5 connections now open).

What else can I do to get connected to MongoDB? Thanks for your help.

Leonid Beschastny

I followed your steps and created new Luminus project using luminus template.

I also studied generated code an found, that default home page is 100% static. So, it shows MongoDB Configuration is Required regardless of whether it actually configured or not:

(defn home-page []
  (layout/render
    "home.html" {:content (util/md->html "/md/docs.md")}))

In other words, it just renders resources/public/md/docs.md into .html and shows it, always the same html page.

As for your configuration, it's absolutely fine.

あなたのためとしてuser.htmlページ、実際の問題があるということiduser-pageしながら、ルート、文字列で、_idデータベース内の数です。だから、(get-replies 2)あなたが呼んでいる代わりに(get-replies "2")文字列化されたを使用する関数または最初に_id着信を解析してみてくださいidread-stringLong/parseLong

(defn user-page [& [id]]
  (layout/render "user.html"
                 {:id id
                  :replies (-> id
                               Long/parseLong ; throws NumberFormatException
                               project-db/get-replies)}))

文字_id列を数値に解析するよりも簡単で安全なので、文字列化されたsを使用することをお勧めします。

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事