Friday, April 26, 2024
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 once I obtain the info, I get an error like this: “error”: “didn’t import jobs” get doesn’t work however submit works code elements are as follows

foremost.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 foremost() {
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 must log the err someplace so you realize 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 possible has info in it explaining why your SQL assertion failed. Whenever you verify 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 basic "didn't retrieve jobs" message right here
    // is okay on 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 ​​should not coming once I get it, clean web page however 200 freezes

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

The log.Print change I urged wasn’t to repair a problem, 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 by some means 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’d assume “freezing” implies that you’re not getting a response in any respect; is that taking place? Or are you maybe working some client-side javascript that’s alleged to do one thing with the response but it surely’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 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 prone to produce errors like these.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments