Sunday, March 19, 2023
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. Once I name the block with id, my block is displayed with out error on the display, however once I name all of the blogs within the knowledge, it provides an invalid date error

principal.go:

kind 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": "did not 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": "did not get blogs"})
			return
		}

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

app.js:

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

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

    async operate fetchSecondData() {
      attempt {
        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;


And the error is…
Simply present it with

err := db.Choose(&blogs, “SELECT * FROM blogs”)
if err != nil {
log.Println(“Error getting blogs:”, err)
c.JSON(http.StatusInternalServerError, gin.H{“error”: “did not get blogs”})
return
}

RELATED ARTICLES

Most Popular

Recent Comments