Friday, April 26, 2024
HomeGolangFetch knowledge with golang api - Technical Dialogue

Fetch knowledge with golang api – Technical Dialogue


I’m making a weblog with golang gin framework relaxation api. After I name the block with id, my block is displayed with out error on the display screen, however after I name all of the blogs within the knowledge, it provides an invalid date error

foremost.go:

sort Weblog struct {
	ID       int        `db:"id" json:"id"`
	Title    string     `db:"title" json:"title"`
	Physique     string     `db:"physique" json:"physique"`
	ImageURL string     `db:"picture" json:"picture"`
	Date     *time.Time `db:"date" json:"date"`
}


// get weblog by ID
	r.GET("/weblog/:id", func(c *gin.Context) {
		var weblog Weblog
		id := c.Param("id")

		// get weblog from database
		err := db.Get(&weblog, "SELECT * FROM blogs WHERE id=$1", id)
		if err != nil {
			if err == sql.ErrNoRows {
				c.JSON(http.StatusNotFound, gin.H{"error": "weblog not discovered"})
			} else {
				c.JSON(http.StatusInternalServerError, gin.H{"error": "didn't get weblog"})
			}
			return
		}

		c.JSON(http.StatusOK, weblog)
	})

	// get all blogs
	r.GET("/blogs", func(c *gin.Context) {
		var blogs []Weblog

		// get all blogs from database
		err := db.Choose(&blogs, "SELECT * FROM blogs")
		if err != nil {
			c.JSON(http.StatusInternalServerError, gin.H{"error": "didn't get blogs"})
			return
		}

		c.JSON(http.StatusOK, blogs)
	})

app.js:

useEffect(() => {
    async perform fetchData() {
      strive {
        const response = await axios.get('http://localhost:8000/weblog/1'); //that is working

        setFirstPost(response.knowledge);
      } catch (error) {
        setError(error.message);
      }
    }
    fetchData();

    async perform fetchSecondData() {
      strive {
        const response = await axios.get('http://localhost:8000/blogs');//not working

        setSecondPost(response.knowledge);
      } catch (error) {
        setError(error.message);
      }
    }
    fetchSecondData();
  }, []);

  return (
    <div className="weblog">
           <div className="blog-page"> 
                <h1> BLOG</h1>
            </div>

      {firstPost && (
        <div className="blog-first">
          <div className="blog-firstpage">
            <img src={firstPost.picture} alt={firstPost.title} />
          </div>
          <div className="blog-text">
            <h2>{firstPost.title}</h2>
            <h3>{new Date(firstPost.date).toLocaleString('en-US', { dateStyle: 'medium' })}</h3>
            <p>{firstPost.physique}</p>
          </div>
          
        </div>
      )}

      {secondPost && (
        <div className="weblog">
          <div className="blog-secondpage">
            <img src={secondPost.picture} alt={secondPost.title} />
          </div>
          <div className="blog-text">
            <h2>{secondPost.title}</h2>
            <h3>{new Date(secondPost.date).toLocaleString('en-US', { dateStyle: 'medium' })}</h3>
            <p>{secondPost.physique}</p>
          </div>
         
        </div>
      )}

      {error && <p>{error}</p>}
    </div>
  );
}

export default Weblog;

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments