Skip to content

Instantly share code, notes, and snippets.

@tehsven
Last active November 21, 2019 20:50
Show Gist options
  • Save tehsven/0235b5f2adf7f41b35baa26d4055b3f2 to your computer and use it in GitHub Desktop.
Save tehsven/0235b5f2adf7f41b35baa26d4055b3f2 to your computer and use it in GitHub Desktop.
Fix dup key error when creating second playlist
From 1cf5b84243e36b207f04ca939cce8b5a42f1a426 Mon Sep 17 00:00:00 2001
From: tehsven <tehsven@gmail.com>
Date: Thu, 21 Nov 2019 12:45:01 -0800
Subject: [PATCH] Fix dup key error when creating second playlist
The passport-local-mongoose plugin creates uniqueness constraints
on a 'username' field for whatever model it's plugged in to.
It was being plugged in to all the models like Artist, Playlist,
and Song instead of just the User model.
The User model is the only model that requires this plugin, since
it's the model that actually does authentication.
The duplicate key error was due the passport-local-mongoose plugin
being plugged in to the Playlist model, creating a uniqueness
constraint on an unnecessarily added 'username' field. Therefore
every time a new playlist was added it would blow up on this
unnecessary uniqueness constraint.
---
models/artist.js | 6 +-----
models/playlist.js | 6 +-----
models/song.js | 7 +------
3 files changed, 3 insertions(+), 16 deletions(-)
diff --git a/models/artist.js b/models/artist.js
index e6d9437..05975d6 100644
--- a/models/artist.js
+++ b/models/artist.js
@@ -1,6 +1,4 @@
var mongoose = require('mongoose');
-var passportLocalMongoose = require("passport-local-mongoose");
-
var ArtistSchema = new mongoose.Schema({
name: String,
@@ -15,6 +13,4 @@ var ArtistSchema = new mongoose.Schema({
}]
});
-ArtistSchema.plugin(passportLocalMongoose);
-
-module.exports = mongoose.model("Artist", ArtistSchema);
\ No newline at end of file
+module.exports = mongoose.model("Artist", ArtistSchema);
diff --git a/models/playlist.js b/models/playlist.js
index 2b0f375..b17040c 100644
--- a/models/playlist.js
+++ b/models/playlist.js
@@ -1,6 +1,4 @@
var mongoose = require('mongoose');
-var passportLocalMongoose = require("passport-local-mongoose");
-
var PlaylistSchema = new mongoose.Schema({
name: String,
@@ -9,6 +7,4 @@ var PlaylistSchema = new mongoose.Schema({
songs: Array
});
-PlaylistSchema.plugin(passportLocalMongoose);
-
-module.exports = mongoose.model("Playlist", PlaylistSchema);
\ No newline at end of file
+module.exports = mongoose.model("Playlist", PlaylistSchema);
diff --git a/models/song.js b/models/song.js
index acd110b..b18a856 100644
--- a/models/song.js
+++ b/models/song.js
@@ -1,7 +1,4 @@
var mongoose = require('mongoose');
-var passportLocalMongoose = require("passport-local-mongoose");
-
-
var SongSchema = new mongoose.Schema({
name: String,
@@ -12,6 +9,4 @@ var SongSchema = new mongoose.Schema({
// }
});
-SongSchema.plugin(passportLocalMongoose);
-
-module.exports = mongoose.model("Song", SongSchema);
\ No newline at end of file
+module.exports = mongoose.model("Song", SongSchema);
--
2.18.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment