Tuesday, March 21, 2023
HomeGolangGolang gin relaxation api publish and get - Getting Assist

Golang gin relaxation api publish and get – Getting Assist


With golang gin relaxation api I create title, physique, date, titles and contents fields, ship to postman and save database. The publish operation is operating efficiently, however after I obtain the information, I get an error like this: “error”: “didn’t import jobs” get doesn’t work however publish works code components are as follows

major.go:

sort Job struct {
    ID       int       `db:"id" json:"id"`
    Title    string    `db:"title" json:"title"`
    Physique     string    `db:"physique" json:"physique"`
    Date     time.Time `db:"date" json:"date"`
    Titles   [4]string `db:"titles" json:"titles"`
    Contents [4]string `db:"contents" json:"contents"`
}

func major() {
r.POST("/job", func(c *gin.Context) {
    var job Job
    if err := c.ShouldBindJSON(&job); err != nil {
        c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
        return
    }

    // insert job into database
    question := "INSERT INTO desk (title, physique,  titles, contents ,date) VALUES ($1, $2, $3,  $4, $5) RETURNING id"
    var id int
    err := db.QueryRow(question, job.Title, job.Physique, pq.Array(job.Titles), pq.Array(job.Contents), time.Now()).Scan(&id)
    if err != nil {
        c.JSON(http.StatusInternalServerError, gin.H{"error": "didn't create job"})
        return
    }

    job.ID = id

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

r.GET("/jobs", func(c *gin.Context) {
    // retrieve all jobs from database
    rows, err := db.Question("SELECT * FROM desk")
    if err != nil {
        c.JSON(http.StatusInternalServerError, gin.H{"error": "didn't retrieve jobs"})
        return
    }
    defer rows.Shut()

    // iterate over rows and retailer in slice of Jobs
    jobs := []Job{}
    for rows.Subsequent() {
        var job Job
        err := rows.Scan(&job.ID, &job.Title, &job.Physique, &job.Date, &job.Titles, &job.Contents)
        if err != nil {
            c.JSON(http.StatusInternalServerError, gin.H{"error": "didn't retrieve jobs"})
            return
        }
        jobs = append(jobs, job)
    }

    c.JSON(http.StatusOK, jobs)
})`


You need to log the err someplace so the underlying trigger.

This error is available in error part : ” didn’t retrieve jobs”

jobs := []Job{}
for rows.Subsequent() {
var job Job
err := rows.Scan(&job.ID, &job.Title, &job.Physique, &job.Date, &job.Titles, &job.Contents)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{“error”: “didn’t retrieve jobs”})
return
}
jobs = append(jobs, job)

// Debugging statements
fmt.Printf("Retrieved job with ID %dn", job.ID)
fmt.Printf("Title: %sn", job.Title)
fmt.Printf("Physique: %sn", job.Physique)
fmt.Printf("Date: %sn", job.Date)
fmt.Printf("Titles: %vn", job.Titles)
fmt.Printf("Contents: %vn", job.Contents)

}

What I imply is your choose assertion returns rows and an err. That err probably has data in it explaining why your SQL assertion failed. Whenever you verify if err != nil, it’s best to log err itself so what the issue is:

rows, err := db.Question("SELECT * FROM desk")
if err != nil {
    c.JSON(http.StatusInternalServerError, gin.H{"error": "didn't retrieve jobs"})
    // Returning a basic "didn't retrieve jobs" message right here
    // is okay to your API, however it's best to log `err` itself
    // someplace in right here:
    //
    // TODO: import "log"
    log.Print("error getting jobs: %v", err)
    return
}

it really works however the values ​​are empty

Are you able to make clear which values are empty?

Values ​​will not be coming after I get it, clean web page however 200 freezes

Sorry, @Coding_Yaz, I’m having a tough time understanding:

The log.Print change I recommended wasn’t to repair a difficulty, it was simply to get extra details about the reason for the SQL assertion failing. Did you see an error and make a change to deal with it, or are you saying that after including a name to log.Print, now you’re not getting an error, however the web site is in some way hanging? Are you getting any output to your console if you run your Go program?

I’m unsure what you imply right here: Are you getting a clean web page and/or a 200 response, or is the web page “freezing?” I’d suppose “freezing” signifies that you’re not getting a response in any respect; is that occuring? Or are you maybe operating some client-side javascript that’s alleged to do one thing with the response nevertheless it’s freezing?

sorry i simply noticed this in debugging

2023/03/21 17:25:21 Error scanning job from database: sql: Scan error on column index 3, title “titles”: unsupported Scan, storing driver.Worth sort string into sort *time.Time

That error appears to point that you simply’re scanning your titles column into your &job.Date subject. I’d suggest as a substitute of writing choose * from ... that you choose the columns explicitly in order that including/eradicating/reordering columns sooner or later is much less prone to produce errors like these.

RELATED ARTICLES

Most Popular

Recent Comments