Cannot get the **_User_** value.
Opened this issue · 15 comments
Deleted user commented
type Order struct {
ID uuid.UUID `json:"id" db:"id"`
CreatedAt time.Time `json:"created_at" db:"created_at"`
UpdatedAt time.Time `json:"updated_at" db:"updated_at"`
UserID uuid.UUID `json:"user_id" db:"user_id"`
Num string `json:"num" db:"num"`
Status string `json:"status" db:"status"`
TotalMoney float64 `json:"total_money" db:"total_money"`
Money float64 `json:"money" db:"money"`
Description string `json:"description" db:"description"`
User User `json:"user" db:"-"`
Goods []Good `json:"goods" db:"-"`
}
type User struct {
ID uuid.UUID `json:"id" db:"id"`
CreatedAt time.Time `json:"created_at" db:"created_at"`
UpdatedAt time.Time `json:"updated_at" db:"updated_at"`
Mobile string `json:"mobile" db:"mobile"`
Role string `json:"role" db:"role"`
Addr string `json:"addr" db:"addr"`
PasswordHash string `json:"-" db:"password_hash"`
Password string `json:"-" db:"-"`
PasswordConfirmation string `json:"-" db:"-"`
}
index.html:
<%= for (order) in orders { %>
<%= order.User.Mobile %>
<% } %>
Cannot get the User value.
markbates commented
Can you add more clarity to this ticket? What error do you get? Is order.User set? What the code that runs all this look like?
…--
Mark Bates
On Jul 27, 2018, 9:20 AM +0100, Paul Zhang ***@***.***>, wrote:
type Order struct {
ID uuid.UUID `json:"id" db:"id"`
CreatedAt time.Time `json:"created_at" db:"created_at"`
UpdatedAt time.Time `json:"updated_at" db:"updated_at"`
UserID uuid.UUID `json:"user_id" db:"user_id"`
Num string `json:"num" db:"num"`
Status string `json:"status" db:"status"`
TotalMoney float64 `json:"total_money" db:"total_money"`
Money float64 `json:"money" db:"money"`
Description string `json:"description" db:"description"`
User User `json:"user" db:"-"`
Goods []Good `json:"goods" db:"-"`
}
type User struct {
ID uuid.UUID `json:"id" db:"id"`
CreatedAt time.Time `json:"created_at" db:"created_at"`
UpdatedAt time.Time `json:"updated_at" db:"updated_at"`
Mobile string `json:"mobile" db:"mobile"`
Role string `json:"role" db:"role"`
Addr string `json:"addr" db:"addr"`
PasswordHash string `json:"-" db:"password_hash"`
Password string `json:"-" db:"-"`
PasswordConfirmation string `json:"-" db:"-"`
}
index.html:
<%= for (order) in orders { %>
<%= _**order.User.Mobile**_ %>
<% } %>
Cannot get the value of user value.
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub, or mute the thread.
Deleted user commented
@markbates When I get the result that use the code
<%=
json(orders)
%>
the result is:
[{"id":"385c55ac-e037-4b59-bf7c-11e72918a594","created_at":"2018-07-27T02:09:31Z","updated_at":"2018-07-27T02:09:31Z","user_id":"680e4342-d533-474d-8239-359b56c2344a","num":"680e4342-d533-474d","status":"active","total_money":200,"money":100,"description":"无","user":{"id":"00000000-0000-0000-0000-000000000000","created_at":"0001-01-01T00:00:00Z","updated_at":"0001-01-01T00:00:00Z","mobile":"","role":"","addr":""},"goods":null}]
The error is that User data is null. but the User has data.
markbates commented
What does the error and the code that sets all this look like?
…--
Mark Bates
On Jul 27, 2018, 9:30 AM +0100, Paul Zhang ***@***.***>, wrote:
@markbates When I get the result that use the code ```
<%=
json(orders)
%>
the result is:
[{"id":"385c55ac-e037-4b59-bf7c-11e72918a594","created_at":"2018-07-27T02:09:31Z","updated_at":"2018-07-27T02:09:31Z","user_id":"680e4342-d533-474d-8239-359b56c2344a","num":"680e4342-d533-474d","status":"active","total_money":200,"money":100,"description":"无","user":{"id":"00000000-0000-0000-0000-000000000000","created_at":"0001-01-01T00:00:00Z","updated_at":"0001-01-01T00:00:00Z","mobile":"","role":"","addr":""},"goods":null}]
The error is that User data is null. but the User has data.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub, or mute the thread.
Deleted user commented
// List gets all Orders. This function is mapped to the path
// GET /orders
func (v OrdersResource) List(c buffalo.Context) error {
// Get the DB connection from the context
tx, ok := c.Value("tx").(*pop.Connection)
if !ok {
return errors.WithStack(errors.New("no transaction found"))
}
var orders []models.Order
// Paginate results. Params "page" and "per_page" control pagination.
// Default values are "page=1" and "per_page=20".
q := tx.PaginateFromParams(c.Params())
// Retrieve all Orders from the DB
if err := q.All(&orders); err != nil {
return errors.WithStack(err)
}
for _, order := range orders {
// find user
var user models.User
err := tx.Find(&user, order.UserID)
if err == nil {
logrus.Infof("mobile:%s", user.Mobile)
order.User = user
}
goodOrders := []models.GoodOrder{}
query := tx.Where("order_id=%s", order.ID.String())
err = query.All(&goodOrders)
if err == nil {
goods := make([]models.Good, len(goodOrders))
for i, goodOrder := range goodOrders {
good := models.Good{}
tx.Find(&good, goodOrder.GoodID)
goods[i] = good
}
order.Goods = goods
}
}
// Add the paginator to the context so it can be used in the template.
c.Set("pagination", q.Paginator)
c.Set("is_orders", true)
return c.Render(200, r.Auto(c, orders))
}
markbates commented
The error, can you please paste the error??
…--
Mark Bates
On Jul 27, 2018, 9:32 AM +0100, Paul Zhang ***@***.***>, wrote:
@markbates
// List gets all Orders. This function is mapped to the path
// GET /orders
func (v OrdersResource) List(c buffalo.Context) error {
// Get the DB connection from the context
tx, ok := c.Value("tx").(*pop.Connection)
if !ok {
return errors.WithStack(errors.New("no transaction found"))
}
var orders []models.Order
// Paginate results. Params "page" and "per_page" control pagination.
// Default values are "page=1" and "per_page=20".
q := tx.PaginateFromParams(c.Params())
// Retrieve all Orders from the DB
if err := q.All(&orders); err != nil {
return errors.WithStack(err)
}
for _, order := range orders {
// find user
var user models.User
err := tx.Find(&user, order.UserID)
if err == nil {
logrus.Infof("mobile:%s", user.Mobile)
order.User = user
}
goodOrders := []models.GoodOrder{}
query := tx.Where("order_id=%s", order.ID.String())
err = query.All(&goodOrders)
if err == nil {
goods := make([]models.Good, len(goodOrders))
for i, goodOrder := range goodOrders {
good := models.Good{}
tx.Find(&good, goodOrder.GoodID)
goods[i] = good
}
order.Goods = goods
}
}
// Add the paginator to the context so it can be used in the template.
c.Set("pagination", q.Paginator)
c.Set("is_orders", true)
return c.Render(200, r.Auto(c, orders))
}
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub, or mute the thread.
Deleted user commented
No error. But the user data is bad.
markbates commented
That’s useful to know. You’re assigning data to a value type, which can’t be mutated, which is why the stuff after you leave the action is empty. Use pointers to mutate data and it’ll work.
…--
Mark Bates
On Jul 27, 2018, 9:35 AM +0100, Paul Zhang ***@***.***>, wrote:
No error. But the user data is bad.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub, or mute the thread.
Deleted user commented
@markbates Which code is bad.
markbates commented
I’m not at a computer, but you’re adding orders.User = User orders is a value type, not a pointer type, so Go disregards those changes when you leave the function. You need to assign your user into a pointer of order not a value type.
…--
Mark Bates
On Jul 27, 2018, 9:47 AM +0100, Paul Zhang ***@***.***>, wrote:
@markbates Which code is bad.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub, or mute the thread.
Deleted user commented
Thanks.
markbates commented
Good luck
…--
Mark Bates
On Jul 27, 2018, 9:50 AM +0100, Paul Zhang ***@***.***>, wrote:
Thanks.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub, or mute the thread.
Deleted user commented
@markbates I think the issue is bug with the paul framework. When change the code
order.User = &models.User{
Mobile: "hello",
}
This User’ mobile value is still empty.
<%=
json(orders)
%>
the result is:
[{"id":"385c55ac-e037-4b59-bf7c-11e72918a594","created_at":"2018-07-27T02:09:31Z","updated_at":"2018-07-27T02:09:31Z","user_id":"680e4342-d533-474d-8239-359b56c2344a","num":"680e4342-d533-474d","status":"active","total_money":200,"money":100,"description":"","user":null,"goods":null}]
markbates commented
It’s the not the User that’s the problem, it’s the order you’re assign it to needs to be a pointer.
…--
Mark Bates
On Jul 27, 2018, 10:12 AM +0100, Paul Zhang ***@***.***>, wrote:
@markbates I think the issue is bug with the paul framework. When change the code
order.User = &models.User{
Mobile: "hello",
}
This User’ mobile value is still empty.
<%=
json(orders)
%>
the result is:
[{"id":"385c55ac-e037-4b59-bf7c-11e72918a594","created_at":"2018-07-27T02:09:31Z","updated_at":"2018-07-27T02:09:31Z","user_id":"680e4342-d533-474d-8239-359b56c2344a","num":"680e4342-d533-474d","status":"active","total_money":200,"money":100,"description":"","user":null,"goods":null}]
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub, or mute the thread.
markbates commented
I’m on vacation with my family, so I can’t get to a computer to help you, sorry. The slack group can probably help you more.
…--
Mark Bates
On Jul 27, 2018, 10:12 AM +0100, Paul Zhang ***@***.***>, wrote:
@markbates I think the issue is bug with the paul framework. When change the code
order.User = &models.User{
Mobile: "hello",
}
This User’ mobile value is still empty.
<%=
json(orders)
%>
the result is:
[{"id":"385c55ac-e037-4b59-bf7c-11e72918a594","created_at":"2018-07-27T02:09:31Z","updated_at":"2018-07-27T02:09:31Z","user_id":"680e4342-d533-474d-8239-359b56c2344a","num":"680e4342-d533-474d","status":"active","total_money":200,"money":100,"description":"","user":null,"goods":null}]
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub, or mute the thread.
Deleted user commented
OK.