With golang gin relaxation api I create title, physique, date, titles and contents fields, ship to postman and save database. The put up operation is operating efficiently, however once I obtain the information, I get an error like this: “error”: “didn’t import jobs” get doesn’t work however put up works code components are as follows
predominant.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 predominant() {
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)
})`
It is best to log the err
someplace so you already know 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
possible has info in it explaining why your SQL assertion failed. If you test if err != nil
, you need to log err
itself so you already know 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 to your API, however you need 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 advised 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 handle 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 someway hanging? Are you getting any output to your console once 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 might 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 however 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 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 might 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 more likely to produce errors like these.