R
Railway•2y ago
Auly

mongodb's collections data can't display on my webpage.

After import a json file, collections have a contacts . when I use "docker run" command, my localhost/contacts don't display any data.
30 Replies
Percy
Percy•2y ago
Project ID: c7cb0789-9eba-454f-8499-d7ee14e026af
Auly
AulyOP•2y ago
c7cb0789-9eba-454f-8499-d7ee14e026af have anyone here?
Brody
Brody•2y ago
are you passing in the mongodb environment variables to the docker run command?
Auly
AulyOP•2y ago
yes, I use "docker run -p 8080:8080 -e PORT=8080 -e MONGODB_URI="mongodb://mongo:[email protected]:7978" hello_crow:latest " And my localhost:8080 display a html fine. @Brody are you still here?
Brody
Brody•2y ago
you have to stop with the spam pings
Auly
AulyOP•2y ago
what relation with spam pings?
Brody
Brody•2y ago
#🛂|readme #5
Auly
AulyOP•2y ago
I am sorry for you. but could you continue help sloving my question?
Brody
Brody•2y ago
chill
Auly
AulyOP•2y ago
ok, my english is not so good. anyway, why contacts under collections do not display on webpage?
Brody
Brody•2y ago
usually the environment variable is called MONGO_URL so use that, both in the command line args and in your code
Auly
AulyOP•2y ago
yes. in main.cpp, use below commands get contacts:
mongocxx::instance inst{};
string mongoConnect=std::string(getenv("MONGODB_URI"));
mongocxx::client conn{mongocxx::uri{mongoConnect}};
auto collection=conn["containers-us-west-175.railway.app:7978"]["contacts"];
mongocxx::instance inst{};
string mongoConnect=std::string(getenv("MONGODB_URI"));
mongocxx::client conn{mongocxx::uri{mongoConnect}};
auto collection=conn["containers-us-west-175.railway.app:7978"]["contacts"];
and use these commands to server contacts page:
CROW_ROUTE(app, "/contacts")
([&collection]()
{
mongocxx::options::find opts;
opts.limit(10);
auto docs=collection.find({}, opts);
std::ostringstream os;
for (auto&& doc : docs)
{
os<<bsoncxx::to_json(doc)<<endl;
}
return crow::response(os.str());
});
CROW_ROUTE(app, "/contacts")
([&collection]()
{
mongocxx::options::find opts;
opts.limit(10);
auto docs=collection.find({}, opts);
std::ostringstream os;
for (auto&& doc : docs)
{
os<<bsoncxx::to_json(doc)<<endl;
}
return crow::response(os.str());
});
Brody
Brody•2y ago
why do you have a hardcoded host and port
Auly
AulyOP•2y ago
i write it according a course, when open 8080 port, the page can display, otherwise, it can't display.
Brody
Brody•2y ago
does this work on localhost?
Auly
AulyOP•2y ago
yes, localhost:8080 and localhost:8080/about have a page.
Brody
Brody•2y ago
show me a screenshot of your railway service variables
Auly
AulyOP•2y ago
Brody
Brody•2y ago
^
Auly
AulyOP•2y ago
so i should only have MONGO_URL.
Brody
Brody•2y ago
your code is not using MONGO_URL
Auly
AulyOP•2y ago
please more in detail
Brody
Brody•2y ago
your code is not using the MONGO_URL environment variable
Auly
AulyOP•2y ago
so i should change string mongoConnect=std::string(getenv("MONGODB_URI")); to MONGODB_URL? and delete other variables?
Brody
Brody•2y ago
i understand english is not your first language, so please read over my message carefully
Auly
AulyOP•2y ago
ok, change to MONGO_URL
Brody
Brody•2y ago
yes
Auly
AulyOP•2y ago
thank you so much.this issue trouble me one day. bye.
Brody
Brody•2y ago
your app works on railway now?
Auly
AulyOP•2y ago
yes, it works after I change getenv and conn[][].I don't know what 's string after '@' in MONGO_URL? I am wrong with conn[string afer@][].
Want results from more Discord servers?
Add your server