Fixed some promise resolves

This commit is contained in:
David Huang 2022-09-24 21:50:49 -04:00
parent 13f2f4b986
commit d195660092

View File

@ -22,6 +22,11 @@ post_thread = async(req) => {
return JSON.stringify(res); return JSON.stringify(res);
} }
for (let i = 0; i < images.length; i++) {
const image = new Image({ data: images[i] });
}
const newForum = new Forum({ const newForum = new Forum({
title: title, title: title,
author: author, author: author,
@ -33,12 +38,22 @@ post_thread = async(req) => {
favorited_by: favorited_by, favorited_by: favorited_by,
}) })
await newForum.save(); newForum.save().then(() => {
console.log("New forum created");
let res = { let res = {
status: 200, status: 200,
body: "Missing parameters", body: "Missing parameters",
} }
return JSON.stringify(res); return JSON.stringify(res);
}).catch(err => {
console.log("New forum error -- not created");
console.error(err);
let res = {
status: 500,
body: "server error"
}
return JSON.stringify(res)
});
} }
catch (err) { catch (err) {
console.error(err); console.error(err);
@ -62,7 +77,7 @@ update_thread = async(req) => {
return JSON.stringify(res); return JSON.stringify(res);
} }
const forum = Forum.findById(id); const forum = await Forum.findById(id);
if (!forum) { if (!forum) {
let res = { let res = {
status: 400, status: 400,
@ -79,7 +94,6 @@ update_thread = async(req) => {
} }
delete_thread = async(req) => { delete_thread = async(req) => {
console.log(req); console.log(req);
const { id } = req; const { id } = req;
@ -91,12 +105,23 @@ delete_thread = async(req) => {
return JSON.stringify(res); return JSON.stringify(res);
} }
await Forum.findByIdAndDelete(id); Forum.findByIdAndDelete(id).then(() => {
console.log("Thread deleted");
let res = { let res = {
status: 200, status: 200,
body: "Ok" body: "Ok"
} }
return JSON.stringify(res); return JSON.stringify(res);
}).catch(err => {
console.log("Forum deletion error");
console.log(err);
let res = {
status: 500,
body: "Server error",
}
return JSON.stringify(res);
});
} }
@ -139,17 +164,35 @@ favorite = async(req) => {
user.favorites.push(forum_id); user.favorites.push(forum_id);
forum.favorited_by++; forum.favorited_by++;
await user.save(); user.save().then(() => {
await forum.save(); console.log("User favorites updated");
forum.save().then(() => {
console.log("Thread favorite updated")
let res = { let res = {
status: 200, status: 200,
body: "Ok" body: "Ok"
} }
return JSON.stringify(res); return JSON.stringify(res);
}).catch(err => {
console.log("Thread favorite error");
console.log(err);
let res = {
status: 500,
body: "Server error",
}
return JSON.stringify(res);
})
}).catch(err => {
console.log("User favorite error");
console.log(err);
let res = {
status: 500,
body: "Server error",
}
return JSON.stringify(res);
});
} }
unfavorite = async(req) => { unfavorite = async(req) => {
const { id, forum_id } = req; const { id, forum_id } = req;
if ( !id || !forum_id ) { if ( !id || !forum_id ) {
@ -178,28 +221,46 @@ unfavorite = async(req) => {
return JSON.stringify(res); return JSON.stringify(res);
} }
let found = user.favorites.indexOf(forum_id); let found = user.favorites.indexOf(forum_id)
if (found < 0) { if (found >= 0) {
let res = { let res = {
status: 400, status: 400,
body: "Thread not favorited", body: "Thread already favorited",
} }
return JSON.stringify(res); return JSON.stringify(res);
} }
user.favorites.splice(found, 1) user.favorites.splice(found, 1);
forum.favorited_by--; forum.favorited_by--;
await user.save(); user.save().then(() => {
await forum.save(); console.log("User unfavorites updated");
forum.save().then(() => {
console.log("Thread unfavorite updated");
let res = { let res = {
status: 200, status: 200,
body: "Ok" body: "Ok"
} }
return JSON.stringify(res); return JSON.stringify(res);
}).catch(err => {
console.log("Thread unfavorite error");
console.log(err);
let res = {
status: 500,
body: "Server error",
}
return JSON.stringify(res);
})
}).catch(err => {
console.log("User unfavorite error");
console.log(err);
let res = {
status: 500,
body: "Server error",
}
return JSON.stringify(res);
});
} }
get_random_thread_list = async(req) => { get_random_thread_list = async(req) => {
} }