Pārlūkot izejas kodu

Fixed some issues with Redis undefined arguments, and some more issues.

KrisVos130 8 gadi atpakaļ
vecāks
revīzija
34b575d78d

+ 1 - 0
backend/index.js

@@ -67,6 +67,7 @@ async.waterfall([
 	if (err && err !== true) {
 		console.error('An error occurred while initializing the backend server');
 		console.error(err);
+		process.exit();
 	} else {
 		console.info('Backend server has been successfully started');
 	}

+ 7 - 1
backend/logic/cache/index.js

@@ -83,9 +83,13 @@ const lib = {
 	 * @param {Boolean} [parseJson=true] - attempt to parse returned data as JSON
 	 */
 	hget: (table, key, cb, parseJson = true) => {
+		if (!key || !table) return cb(null, null);
 		lib.client.hget(table, key, (err, value) => {
 			if (err) return typeof cb === 'function' ? cb(err) : null;
-			if (parseJson) try { value = JSON.parse(value); } catch (e) {}
+			if (parseJson) try {
+				value = JSON.parse(value);
+			} catch (e) {
+			}
 			if (typeof cb === 'function') cb(null, value);
 		});
 	},
@@ -98,6 +102,7 @@ const lib = {
 	 * @param {Function} cb - gets called when the value has been deleted from Redis or when it returned an error
 	 */
 	hdel: (table, key, cb) => {
+		if (!key || !table) return cb(null, null);
 		lib.client.hdel(table, key, (err) => {
 			if (err) return typeof cb === 'function' ? cb(err) : null;
 			if (typeof cb === 'function') cb(null);
@@ -112,6 +117,7 @@ const lib = {
 	 * @param {Boolean} [parseJson=true] - attempts to parse all values as JSON by default
 	 */
 	hgetall: (table, cb, parseJson = true) => {
+		if (!table) return cb(null, null);
 		lib.client.hgetall(table, (err, obj) => {
 			if (err) return typeof cb === 'function' ? cb(err) : null;
 			if (parseJson && obj) Object.keys(obj).forEach((key) => { try { obj[key] = JSON.parse(obj[key]); } catch (e) {} });

+ 17 - 0
backend/logic/playlists.js

@@ -13,6 +13,23 @@ module.exports = {
 	 */
 	init: cb => {
 		async.waterfall([
+			(next) => {
+				cache.hgetall('playlists', next);
+			},
+
+			(playlists, next) => {
+				if (!playlists) return next();
+				let playlistIds = Object.keys(playlists);
+				async.each(playlistIds, (playlistId, next) => {
+					db.models.playlist.findOne({_id: playlistId}, (err, playlist) => {
+						if (err) next(err);
+						else if (!playlist) {
+							cache.hdel('playlists', playlistId, next);
+						}
+					});
+				}, next);
+			},
+
 			(next) => {
 				db.models.playlist.find({}, next);
 			},

+ 1 - 0
backend/logic/songs.js

@@ -20,6 +20,7 @@ module.exports = {
 			},
 
 			(songs, next) => {
+				if (!songs) return next();
 				let songIds = Object.keys(songs);
 				async.each(songIds, (songId, next) => {
 					db.models.song.findOne({_id: songId}, (err, song) => {