<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: pawan-kr1997</title>
    <description>The latest articles on DEV Community by pawan-kr1997 (@pawankr1997).</description>
    <link>https://dev.to/pawankr1997</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F747101%2F8e3dc0f2-2a1c-4729-9213-3a3721c03e79.png</url>
      <title>DEV Community: pawan-kr1997</title>
      <link>https://dev.to/pawankr1997</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/pawankr1997"/>
    <language>en</language>
    <item>
      <title>Why I am getting the error "no database found"?</title>
      <dc:creator>pawan-kr1997</dc:creator>
      <pubDate>Tue, 07 Dec 2021 10:05:22 +0000</pubDate>
      <link>https://dev.to/pawankr1997/why-i-am-getting-the-error-no-database-found-5hk0</link>
      <guid>https://dev.to/pawankr1997/why-i-am-getting-the-error-no-database-found-5hk0</guid>
      <description>&lt;p&gt;I am using mongodb as my database. I am trying to save the details(title, price and description) of a product to my database.&lt;/p&gt;

&lt;p&gt;Here is my database.js code&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const mongodb= require('mongodb');

const MongoClient = mongodb.MongoClient;

let _db;

const mongoConnect= callback=&amp;gt;{
    MongoClient.connect('mongodb+srv://pawan:*************@cluster0.vchvo.mongodb.net/myFirstDatabase?retryWrites=true&amp;amp;w=majority')
    .then(client=&amp;gt;{
        console.log('connected');
        _db= client.db();
        callback();
    })
    .catch(err=&amp;gt;{
        console.log(err);
        throw err;
    })
}

const getDb=()=&amp;gt;{
    if(_db){
        return _db;
    }
    throw 'no database found';
};

exports.mongoConnect= mongoConnect;
exports.getDb= getDb;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;(I have placed "*******" in place of password only for question). I could say that my database connection has been made as I can see the Connected message on my console.&lt;/p&gt;

&lt;p&gt;Here is the save() function implementation in the product.js in models&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;save() {
        const db = getDb();
        return db.collection('products')
            .insertOne(this)
            .then(result=&amp;gt;{
                console.log(result);
            })
            .catch(err=&amp;gt;{
                console.log("error from save in model"+ err);
            })
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here is the controller implementation:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;exports.postAddProduct = (req, res, next) =&amp;gt; {
    const title=req.body.title;
    const price=req.body.price;
    const description=req.body.description;

    const product = new Product(title, price, description);
    product.save()
        .then(result =&amp;gt; {
            console.log("Created product:  "+ result);
            res.redirect('/');
            }
        )
        .catch(err =&amp;gt; {
            console.log(err);
        })

}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Upon running my code when I submit the details of the product instead of being redirected to "/", I am getting a message as "no database found".&lt;/p&gt;

&lt;p&gt;Please guide me to resolve this problem.&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
