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

Golang gin relaxation api submit 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 submit operation is working efficiently, however after I obtain the info, I get an error like this: “error”: “didn’t import jobs” get doesn’t work however submit works code components are as follows

fundamental.go:

kind 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 fundamental() {
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 you realize the underlying trigger.



1 Like

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 seemingly has info in it explaining why your SQL assertion failed. While you examine if err != nil, it’s best to log err itself so you realize 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 normal "didn't retrieve jobs" message right here
    // is okay in 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 ​​aren’t 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 steered 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 website is someway hanging? Are you getting any output to your console if you run your Go program?

I’m undecided what you imply right here: Are you getting a clean web page and/or a 200 response, or is the web page “freezing?” I might suppose “freezing” implies that you’re not getting a response in any respect; is that occuring? Or are you maybe working some client-side javascript that’s purported 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, identify “titles”: unsupported Scan, storing driver.Worth kind string into kind *time.Time

That error appears to point that you just’re scanning your titles column into your &job.Date discipline. I might suggest as an alternative of writing choose * from ... that you choose the columns explicitly in order that including/eradicating/reordering columns sooner or later is much less more likely to produce errors like these.

RELATED ARTICLES

Most Popular

Recent Comments