dora12345678 2018-06-17 12:35
浏览 26
已采纳

如何更改sqlite的get功能?

how can I change my Get function so that it just returns one Equipment-Objekt?

func GetEquipmentByID(Id string) (equipment Equipment, err error) {
    equipment = Equipment{}
    err = Db.QueryRow("select ID, Name, Description, ImgPath, Category, Availability, Amount, Storage from Equipment where Id = $1", Id).Scan(&equipment.ID, &equipment.Name, &equipment.Description, &equipment.ImgPath, &equipment.Category, &equipment.Availability, &equipment.Amount, &equipment.Storage)
    return 
}

I want to use this function to fill an Equipment Array in my Controller. But now it doesnt work, because my function returns 2 objects (Equipment and Error)

I want to use it here:

    func Cart(w http.ResponseWriter,    r   *http.Request)  {
        data := CartData{ 
        Name: "Cart",
        Equipment: model.GetEquipment(model.Db),
        CartList: nil,  
        Pages: []Page{
            {
                Title: "Meine Geräte", 
                Active: false,
                Link: "/my-equipment",
            },
            {
                Title: "Equipment", 
                Active: false,
                Link: "/equipment",
            },
            {
                Title: "Logout",
                Active: false,
                Link: "/logout",
            },
        },

    }

        session, _ := store.Get(r, "cookie-name")


      list := session.Values["EquipmentIDs"].(string)

      result := strings.Split(list, ",")
      for i := range result {
            fmt.Print(i)
            fmt.Println(result[i])
        }



     for i:= range result {
equipment, err := model.GetEquipmentByID(result[i])
if err != nil {
    // handle error
}
data.CartList[i] = equipment // ERROR BECAUSE OF THIS LINE
}



        tmpl:= template.Must(template.ParseFiles("template/base_user.html", "template/cart.html"))
        tmpl.ExecuteTemplate(w, "base", data)
        }
    }

So my second question is, how can I pass the array in my cart function as data

  • 写回答

2条回答 默认 最新

  • duanqinbi9029 2018-06-17 17:17
    关注

    The append-method solved my problem.

        session, _ := store.Get(r, "cookie-name")
        list := session.Values["EquipmentIDs"].(string)
    
          result := strings.Split(list, ",")
          for i := range result {
                fmt.Print(i)
                fmt.Println(result[i])
            }
    
    var List[]model.Equipment
    
            for i := range result {
            equipment, err := model.GetEquipmentByID(result[i])
            if err != nil {
                // handle error
            }
            fmt.Println(i)
            fmt.Println(equipment)
            List = append(List,equipment)
        //  List[i] = equipment
        //  data.CartList[i] = equipment
            }
    
    
    fmt.Println(List)
        data := CartData{ 
        Name: "Cart",
        Equipment: model.GetEquipment(model.Db),
        CartList: List,
        Pages: []Page{
            {
                Title: "Meine Geräte", 
                Active: false,
                Link: "/my-equipment",
            },
            {
                Title: "Equipment", 
                Active: false,
                Link: "/equipment",
            },
            {
                Title: "Logout",
                Active: false,
                Link: "/logout",
            },
        },
    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法