LoginSignup
12
7

More than 5 years have passed since last update.

[Node.js]HerokuでMongoDBを使う方法

Last updated at Posted at 2018-02-11

HerokuでMongoDBを使うのに手こずったので、解説したいと思います。

前準備

クレジットカード情報を追加していないと、下記のエラーが出てしますので登録しておきましょう。
Item could not be created:
Please verify your account to install this add-on plan
(please enter a credit card) For more information,
see https://devcenter.heroku.com/categories/billing
Verify now at https://heroku.com/verify

・設定方法
Herokuの画面右上にある、自身のアイコンをクリックし、「Account setting」から「Bilding」に進むとクレジットカード情報を追加することができます。

MongoDBの導入

  1. 自身のアプリケーション一覧から、任意のアプリケーションを選択する。
  2. 「Resouces」タブを選択し、「Find more add-ons」をクリックする。
  3. 新しいWebページが表示されると思うので、「Data Stores」の中から、「mLab MongoDB」を選択する。
  4. 「Install mLab MongoDB」をクリックすると、どのアプリケーションにインストースするのか問われるので、好きなものを選択してください。

デプロイに向けての設定

  1. コマンドプロントからheroku config -a {アプリ名}を実行し、MONGODB_URIの値を調べる。
  2. mongoose.connect()の引数にMONGODB_URIの値を渡せば、デプロイに成功すると思います。

まとめ

初めて、Herokuのアドオンを追加したが、他にも色々なものが追加できるので遊べそう。

12
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
12
7