From 3b22f15a45a08bb79e490dc87159248a5834e141 Mon Sep 17 00:00:00 2001 From: Raymonzut <40148684+Raymonzut@users.noreply.github.com> Date: Sat, 25 Apr 2020 16:38:28 +0200 Subject: [PATCH] Extend remote to accept post id --- client/src/remote.js | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/client/src/remote.js b/client/src/remote.js index 6b6ac99..63565e1 100644 --- a/client/src/remote.js +++ b/client/src/remote.js @@ -1,9 +1,20 @@ module.exports = { - getPosts: function() { + getPosts: function(id) { + const BASE_URL = 'http://localhost:5000/api/posts' + const URL = BASE_URL + (id ? `/${id}` : '?sort=-1') + this.$http - .get('http://localhost:5000/api/posts?sort=-1') + .get(URL) .then(res => { - this.posts = res.body + if (id) { + if (res.body.length === 0) { + throw Error("Response body empty") + } + this.post = res.body[0] + } + else { + this.posts = res.body + } }) .catch(err => { console.log(`Error: ${err}`)