dersar00

dersar00

Failed save to database

Hello!
Git Repository here.
I have a problem with saving data to database. I use mongodb_ecto adapter, when I try save it through iex console I can do it:

iex(1)> Friends.PersonController.save()
START SAVING
SAVED

09:58:17.706 [debug] QUERY OK db=0.4ms
INSERT coll="people" document=[age: 11, _id: #BSON.ObjectId<5b027c190d1a8f0f042d5600>] []
:ok

or

iex(1)> person = %Friends.Person{}
%Friends.Person{
  __meta__: #Ecto.Schema.Metadata<:built, "people">,
  age: nil,
  first_name: nil,
  id: nil,
  last_name: nil
}
iex(2)> Friends.Repo.insert(person)

10:50:43.451 [debug] QUERY OK db=0.4ms
INSERT coll="people" document=[_id: #BSON.ObjectId<5b0288630d1a8f06ef2e3699>] []
{:ok,
 %Friends.Person{
   __meta__: #Ecto.Schema.Metadata<:loaded, "people">,
   age: nil,
   first_name: nil,
   id: "5b0288630d1a8f06ef2e3699",
   last_name: nil
 }}

But if uncomment Friends.PersonController.save and try to call it when program is work, I got error:

⇒ mix
Compiling 1 file (.ex)
START SAVING

== Compilation error in file lib/friends.ex ==
** (ArgumentError) argument error
    (stdlib) :ets.lookup_element(Mongo.IdServer, :machineprocid, 2)
    lib/mongo/id_server.ex:50: Mongo.IdServer.new/0
    lib/ecto/repo/schema.ex:651: Ecto.Repo.Schema.autogenerate_id/4
    lib/ecto/repo/schema.ex:199: anonymous fn/13 in Ecto.Repo.Schema.do_insert/4
    lib/friends/person_controller.ex:11: Friends.PersonController.save/0
    lib/friends.ex:4: (module)

What’s the solution for it? How I can do it?
I have an assumption that when I run the program, I just do not start Mongo?

Most Liked Responses

Nicd

Nicd

That works on its own but not in a project where you have other applications that need starting up. In addition, the example in the documentation deals with exs (script mode) files, which are run like scripts. Normally your ex files are compiled first and then executed.

You need to look at how Elixir projects are started using a supervision tree. Unfortunately I’m quite busy right now so I can’t link any tutorial but I hope others can help in this regard. Basically what you want is a supervision tree that starts the necessary applications (like your Ecto.Repo), then starts a process with that RabbitMQ listener code.

Nicd

Nicd

The code at the end of the file (outside the defmodule) is again run at compile time. I suggest you read up on how Elixir programs are started as to where you should put your code. You cannot put code just anywhere, because at compile time your applications are not running.

Where Next?

Popular in Questions Top

vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
jaysoifer
Is there a way to rollback a specific migration and only that one (“skipping” all the other ones)? Would mix ecto.rollback -v 200809061...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New

Other popular topics Top

albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
Nvim
Anybody knows a comprehensive comparison of Django and Phoenix, thanks for the help. Where are they similar? Where do they differ the m...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New

We're in Beta

About us Mission Statement