Sunday, March 19, 2023
HomeGolangFetch information with golang api - Technical Dialogue

Fetch information 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 after I name all of the blogs within the information, it provides an invalid date error

major.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": "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() {
      strive {
        const response = await axios.get('http://localhost:8000/weblog/1'); //that is working

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

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

        setSecondPost(response.information);
      } 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
}

no error it really works nice however the show reveals an invalid date

SELECT * FROM blogs signifies that you could possibly fetch a number of rows. However your Go code signifies that just one row is fetched. And your template signifies no .vary of rows is displayed.

So when you break up your questions into three

  1. Does the question works in pgAdmin (or related)? What number of rows?
  2. Does Go fetches the info appropriate?
  3. Does the template reveals the info appropriate? Does it iterate (.vary) over the rows accurately?

Or do I miss one thing?

RELATED ARTICLES

Most Popular

Recent Comments