title: Unexpected end of JSON Input while parsing json file read asynchronously
body: <p>I am new to node js and using MVC pattern in my project so when I am trying to add products to my cart.json file I am facing the issue</p>
  
  <pre><code>routes file
  
  const Product = require('../models/product');
  const Cart = require('../models/cart')
  
  exports.postcart = (req,res,next) =&gt; {
      const proid = req.params.productcartid
      Product.findbyid(proid).then(function(value) {
          console.log(value.price)    // prints the price correctly so the producted is received as an argument
          Cart.addProduct(proid, value.price)
      })
  }
  </code></pre>
  
  <p>Product model</p>
  
  <pre><code>  module.exports = class Product {
        // constructor function here 
  
        save() {
            //saving the products here, works fine
        }
  
        static findbyid(id) {
            const p = path.join(path.dirname(process.mainModule.filename), 'data', 'products.json') 
            return new Promise((resolve, reject) =&gt; {
                 fs.readFile(p, (err, data) =&gt; {
                     const allproducts = JSON.parse(data)
                     const result = allproducts.find(p =&gt; p.id === id)
                     return resolve(result)   
                 })
            })
        } 
    }
  </code></pre>
  
  <p>Cart model</p>
  
  <pre><code>module.exports = class Cart {
  static addProduct(id, price) {
      const p=path.join(path.dirname(process.mainModule.filename),
  'data','cart.json')
      fs.readFile(p, (error, file) =&gt; {
          console.log('sgdf',id);
          let cart = {
              products: [],
              totalPrice: 0
          };
          if(!error) {
              console.log('inside error') //prints in console
              cart = JSON.parse(file);
          }
          console.log('outside error') //does not output this and anything below from here too doesn't prints in the console
  
          const existingProductIndex = cart.products.findIndex(p =&gt; 
  p.id === id);
          console.log('dghjhjghfg',existingProductIndex) ////does not output this 
          const existingProduct = 
  cart.products[existingProductIndex];
          if(existingProduct) {
              existingProduct.qty += 1;
          }
          else {
              cart.products.push({
                  id,
                  qty: 1
              });
          }
          cart.totalPrice += Number(price);
          console.log(cart);
          fs.writeFile(p, JSON.stringify(cart), error =&gt; {
              if(error) return console.error(error);
          });
      });
  }
  </code></pre>
  
  <p>};</p>
  
  <p>cart.js</p>
  
  <pre><code>const fs=require('fs')
  const path=require('path')
  module.exports = class Cart {
  
  static addProduct(id, price) {
      const 
   p=path.join(path.dirname(process.mainModule.filename),
  'data','cart.json')
      fs.readFile(p, (error, file) =&gt; {
          console.log('sgdf',id);
          let cart = {
              products: [],
              totalPrice: 0
          };
          if(!error) {
              console.log('insdie error')
              console.log(file)
              cart = JSON.parse(file)
          }
          console.log('outside error')
  
          const existingProductIndex = cart.products.findIndex(p =&gt; p.id 
    === id);
          console.log('dghjhjghfg',existingProductIndex)
          const existingProduct = cart.products[existingProductIndex];
          if(existingProduct) {
              existingProduct.qty += 1;
          }
          else {
              cart.products.push({
                  id,
                  qty: 1
              });
          }
          console.log('adsgfdgfh')
          cart.totalPrice += Number(price);
          console.log(cart);
          fs.writeFile(p, JSON.stringify(cart), error =&gt; {
              if(error) return console.error(error);
          });
      });
      }
     };
  </code></pre>
  
  <p>The error i am facing is </p>
  
  <pre><code>undefined:1
  
  
  
  SyntaxError: Unexpected end of JSON input
  at JSON.parse (&lt;anonymous&gt;)
  at /Users/ratnabhkumarrai/Desktop/max-project/models/cart.js:15:29
  at FSReqCallback.readFileAfterClose [as oncomplete] 
  (internal/fs/read_file_context.js:61:3)
  </code></pre>
  
  <p>In my Cart models when i try to console log and see i find that only my half of the code is executed the console log that i tried below does not get executed....What am i doing wrong here ?</p>