<?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: Sakibul Anwar</title>
    <description>The latest articles on DEV Community by Sakibul Anwar (@sakiblite).</description>
    <link>https://dev.to/sakiblite</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.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F713427%2F3a8a1e76-c781-4aac-b709-6a90c055b211.jpg</url>
      <title>DEV Community: Sakibul Anwar</title>
      <link>https://dev.to/sakiblite</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/sakiblite"/>
    <language>en</language>
    <item>
      <title>Mongoose multiple references for one property (dynamic references via refPath)</title>
      <dc:creator>Sakibul Anwar</dc:creator>
      <pubDate>Wed, 27 Jul 2022 10:30:00 +0000</pubDate>
      <link>https://dev.to/sakiblite/mongoose-multiple-references-for-one-property-dynamic-references-via-refpath-2bnc</link>
      <guid>https://dev.to/sakiblite/mongoose-multiple-references-for-one-property-dynamic-references-via-refpath-2bnc</guid>
      <description>&lt;p&gt;Let's assume we have two models&lt;br&gt;
1.&lt;strong&gt;Mobile&lt;/strong&gt;&lt;br&gt;
2.&lt;strong&gt;Laptop&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;We want to add review feature for customers in our application. For one review model there will be multiple references. &lt;br&gt;
This process is bit complicated, but we will go through this.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Create &lt;strong&gt;Review&lt;/strong&gt; model
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
const mongoose = require('mongoose');
const reviewSchema = new mongoose.Schema(
  {
    review: {
      type: String,
      required: [true, 'Review is required'], //Review: Good product
      min: 3,
      max: 200,
    },
    rating: {
      type: Number,
      required: [true, 'Review is required'], //Rating: 5
      min: 1,
      max: 5,
      default: 4,
    },
    product: {
      type: mongoose.Schema.Types.ObjectId,
      refPath: 'onModel', // product =&amp;gt; mobile,laptop
      required: [true, 'Review must belong to a product'],
    },
    onModel: {
      type: String,
      required: true,
      enum: ['Laptop', 'Mobile'],
    },
    user: {
      type: mongoose.Schema.Types.ObjectId,
      ref: 'User',
      required: [true, 'Review must belong to a user'], //user who reviewed your product
    },
    createdAt: {
      type: Date,
      default: Date.now(),
    },
  },
  {
    toJSON: { virtuals: true },
    toObject: { virtuals: true }, //for virtual property. 
  }
);

reviewSchema.pre(/^find/, function (next) {
  this.populate({ path: 'product', select: 'name  image slug' }); //this will populate the product details in the review route for all the find operations. example: api/review
  this.populate({
    path: 'user',
    select: 'name image',
  }); //this will populate the user details in the review route
  next();
});

const Review = mongoose.model('Review', reviewSchema);

module.exports = Review;

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

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Create &lt;strong&gt;Review&lt;/strong&gt; middleware for &lt;strong&gt;post&lt;/strong&gt; request
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;exports.createReview=async (req,res,next)=&amp;gt;{
try{
    const { review,rating,user,onModel,product} = req.body;
 await Review.create({review,rating,user,onModel,product})

// logic
}catch(err){
//error logic
}
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fi4ribnla49bdombpq6p5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fi4ribnla49bdombpq6p5.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Virtual Populate in &lt;strong&gt;Mobile&lt;/strong&gt; model
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; const mobileSchema = new mongoose.Schema(
  {
//logic
  },
  {
    toJSON: { virtuals: true },
    toObject: { virtuals: true },
  }
);

//virtual populating
mobileSchema.virtual('reviews', {
  ref: 'Review', //referencing review model
  localField: '_id',  //mobile object id
  foreignField: 'product', //Review model field for mobile object id
});

const Mobile = mongoose.model('Mobile', mobileSchema);
module.exports = Mobile;

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

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Populating reviews of &lt;strong&gt;Mobile&lt;/strong&gt; model
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;exports.getMobileById=async (req,res,next)=&amp;gt;{
try{

const mobile=await Mobile.findById(req.params.id).populate({ path: 'reviews', select: 'review rating user createdAt'});

//other logics

}catch(err){
//error logic
}
}  
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fieb4o3tmikbukthhpzc8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fieb4o3tmikbukthhpzc8.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Thank you for reading &lt;/p&gt;

&lt;p&gt;Connect With Me : &lt;a href="https://github.com/Sakib-lite" rel="noopener noreferrer"&gt;Github&lt;/a&gt; &lt;a href="//www.linkedin.com/in/sakibul-anwar-5b06ab181"&gt;LinkedIn&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;#mongoose #dynamic #reference&lt;/p&gt;

</description>
    </item>
    <item>
      <title>How to use Notistack snackbar outside of react components</title>
      <dc:creator>Sakibul Anwar</dc:creator>
      <pubDate>Mon, 25 Jul 2022 15:38:00 +0000</pubDate>
      <link>https://dev.to/sakiblite/how-to-use-notistack-snackbar-outside-of-react-components-3ln1</link>
      <guid>https://dev.to/sakiblite/how-to-use-notistack-snackbar-outside-of-react-components-3ln1</guid>
      <description>&lt;p&gt;Notistack is great tool for handling notifications. Sometimes we create a separate js file for calling api. But normally it is not available outside of react components. &lt;/p&gt;

&lt;p&gt;Today we are gonna solve this issue.For this we have to follow couple of steps.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;I'm showing this for NextJS&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;u&gt;&lt;strong&gt;JavaScript&lt;/strong&gt;&lt;/u&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

import { useSnackbar } from 'notistack';

let useSnackbarRef;
export const SnackbarUtilsConfigurator = () =&amp;gt; {
  useSnackbarRef = useSnackbar();
  return null;
};

export default {
  success(msg) {
    this.toast(msg, 'success');
  },
  warning(msg) {
    this.toast(msg, 'warning');
  },
  info(msg) {
    this.toast(msg, 'info');
  },
  error(msg) {
    this.toast(msg, 'error');
  },
  toast(msg, variant = 'default') {
    useSnackbarRef.enqueueSnackbar(msg, { variant,    autoHideDuration: 1500 });
  },
};


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

&lt;/div&gt;

&lt;p&gt;&lt;u&gt;&lt;strong&gt;TypeScript&lt;/strong&gt;&lt;/u&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

import { useSnackbar, VariantType, WithSnackbarProps } from 'notistack'

let useSnackbarRef: WithSnackbarProps
export const SnackbarUtilsConfigurator: React.FC = () =&amp;gt; {
  useSnackbarRef = useSnackbar()
  return null
}

export default {
  success(msg: string) {
    this.toast(msg, 'success')
  },
  warning(msg: string) {
    this.toast(msg, 'warning')
  },
  info(msg: string) {
    this.toast(msg, 'info')
  },
  error(msg: string) {
    this.toast(msg, 'error')
  },
  toast(msg: string, variant: VariantType = 'default') {
    useSnackbarRef.enqueueSnackbar(msg, { variant })
  }
}


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

&lt;/div&gt;

&lt;blockquote&gt;
&lt;p&gt;Import SnackbarProvider from notistack&lt;br&gt;
Import the SnackbarUtilsConfigurator  function in _app.js.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftqxndo7kix3itqy7s5if.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftqxndo7kix3itqy7s5if.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Example&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fv7yil4c0keb9ysszxfxa.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fv7yil4c0keb9ysszxfxa.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Thank you for reading &lt;/p&gt;

&lt;p&gt;Connect With Me : &lt;a href="https://github.com/Sakib-lite" rel="noopener noreferrer"&gt;Github&lt;/a&gt; &lt;a href="//www.linkedin.com/in/sakibul-anwar-5b06ab181"&gt;LinkedIn&lt;/a&gt;&lt;/p&gt;

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