gobuffalo/plush

Cannot get the **_User_** value.

Opened this issue · 15 comments

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 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

// 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))
}

No error. But the user data is bad.

@markbates Which code is bad.

Thanks.

@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}]