{"id":2476,"date":"2016-01-05T06:55:19","date_gmt":"2016-01-05T06:55:19","guid":{"rendered":"http:\/\/www.nikola-breznjak.com\/blog\/?p=2476"},"modified":"2016-01-05T07:03:27","modified_gmt":"2016-01-05T07:03:27","slug":"connect-to-mongodb-server-ionic","status":"publish","type":"post","link":"https:\/\/nikola-breznjak.com\/blog\/stack-overflow\/connect-to-mongodb-server-ionic\/","title":{"rendered":"Connect to MongoDB remote server with Ionic"},"content":{"rendered":"<p><a href=\"http:\/\/stackoverflow.com\/users\/534755\/nikola\"><img loading=\"lazy\" decoding=\"async\" title=\"profile for Nikola at Stack Overflow, Q&amp;A for professional and enthusiast programmers\" src=\"http:\/\/stackoverflow.com\/users\/flair\/534755.png\" rel=\"lightbox[2476]\" alt=\"profile for Nikola at Stack Overflow, Q&amp;A for professional and enthusiast programmers\" width=\"208\" height=\"58\" \/><\/a><br \/>\nI&#8217;m a big fan of <a href=\"http:\/\/stackoverflow.com\/\">Stack Overflow<\/a> and I tend to contribute regularly (am currently in the <a href=\"http:\/\/stackexchange.com\/leagues\/1\/alltime\/stackoverflow\/2008-07-31\/534755?sort=reputationchange#534755\">top 0.X%<\/a>).\u00a0In this category (<a href=\"http:\/\/www.nikola-breznjak.com\/blog\/category\/stack-overflow\/\">stackoverflow<\/a>)\u00a0of posts, I will be posting my top rated questions and answers. This, btw, is allowed as explained in the meta thread <a href=\"http:\/\/meta.stackoverflow.com\/questions\/266971\/can-i-post-so-questions-and-answers-in-a-personal-blog\/266973\">here<\/a>.<\/p>\n<p>As you may know, I&#8217;m really into Ionic framework lately\u00a0and am helping out on StackOverflow with the knowledge I gained so far with the framework. I&#8217;m currently <strong>#1<\/strong> in the <a href=\"http:\/\/stackoverflow.com\/tags\/ionic\/topusers\">top All time answerers<\/a> list.<\/p>\n<p>I answered <a href=\"http:\/\/stackoverflow.com\/questions\/34160525\/connect-to-mongodb-remote-server-with-ionic\/34161820\">this question<\/a>\u00a0by user <a href=\"http:\/\/stackoverflow.com\/users\/1415805\/antifa\">Antifa<\/a>:<\/p>\n<p>I&#8217;m trying to develop an application in phonegap\/ionic and I want to use a remote database to store my data. I want to use MongoDB. I already tried lots of ways such as <a href=\"https:\/\/www.npmjs.com\/package\/mongodb\" rel=\"nofollow\">this<\/a> quick start guide.<\/p>\n<p>By using this I can connect to DB but this is pure node.js and the <code>require()<\/code> function cannot be recognized when I try to add this code to an ionic starter app. Maybe <code>require<\/code> is not supported in ionic<\/p>\n<p>Is there a way that can achieve it in Ionic?<\/p>\n<p>Please let me know if you want some more information.<\/p>\n<p><a href=\"http:\/\/stackoverflow.com\/questions\/34160525\/connect-to-mongodb-remote-server-with-ionic\/34161820#34161820\">My answer<\/a> was:<\/p>\n<blockquote><p>The thing is; you can&#8217;t connect Ionic to MongoDB directly. No, there&#8217;s not workaround, no magic involved, it&#8217;s just not the way this is supposed to work. Ionic works on top of Angular and Angular is a frontend framework.<\/p>\n<p>However, the way you should do it is that you basically create a (RESTful) API on your server side.<\/p>\n<p>Most likely this will be made with Node.js which will talk directly to MongoDB and query it. A framework very well suited for this (you actually may be using it already) is <a href=\"http:\/\/expressjs.com\/en\/index.html\" rel=\"nofollow\">Express<\/a>.<\/p>\n<p>After you write your (RESTful) API then you can consume it through your services in Angular by using Angular&#8217;s <a href=\"https:\/\/docs.angularjs.org\/api\/ngResource\/service\/$resource\" rel=\"nofollow\">$resource<\/a> object.<\/p>\n<p>Sure, this is not a step by step answer, and it seems you&#8217;re just starting in this area, so you have some learning do to on your part (REST, RESTful, $resource, services&#8230;), but I wish you good luck and if you&#8217;ll have any more specific questions, don&#8217;t hesitate to ask them.<\/p>\n<p>https:\/\/twitter.com\/HitmanHR\/status\/684267579976904704<\/p><\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;m a big fan of Stack Overflow and I tend to contribute regularly (am currently in the top 0.X%).\u00a0In this category (stackoverflow)\u00a0of posts, I will be posting my&hellip;<\/p>\n","protected":false},"author":1,"featured_media":1991,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[43,35],"tags":[],"class_list":["post-2476","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-ionic","category-stack-overflow"],"_links":{"self":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts\/2476","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/comments?post=2476"}],"version-history":[{"count":3,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts\/2476\/revisions"}],"predecessor-version":[{"id":2479,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts\/2476\/revisions\/2479"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/media\/1991"}],"wp:attachment":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/media?parent=2476"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/categories?post=2476"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/tags?post=2476"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}