Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

using Documentbase.Update() #20

Open
fabulousduck opened this issue Apr 18, 2018 · 1 comment
Open

using Documentbase.Update() #20

fabulousduck opened this issue Apr 18, 2018 · 1 comment
Assignees

Comments

@fabulousduck
Copy link

I am trying to update a defined model with the following piece of code

	connection := context.MustGet("dbConnection").(*mongodm.Connection)
	Reservation := connection.Model("reservation")
	reservation := &models.Reservation{}

	err := context.BindJSON(r)
	if err != nil {
		context.AbortWithStatusJSON(500, apierrors.JsonBindingError)
		return
	}

	err = Reservation.FindId(id).Exec(reservation)
	if err != nil {
		context.AbortWithStatusJSON(500, apierrors.DBLookupFailedError)
	}

	err, _ = reservation.Update(bson.M{
		"IsSharedRoom":      r.IsSharedRoom,
		"Beds":              r.Beds,
		"PartySize":         r.PartySize,
		"notes":             r.Notes,
		"ConfirmedGuest":    r.ConfirmedGuest,
		"RoomType":          r.RoomType,
		"CheckinDate":       r.CheckinDate,
		"CheckoutDate":      r.CheckoutDate,
		"ReservationNumber": r.ReservationNumber,
	})

however, when this code is ran, nothing in the database is updated on the model, is there something I am missing ? I also could not find any documentation on how to use this function other than that it can be used on an IDocumentBase

Regards Duck

@moehlone
Copy link
Contributor

As long as I can remember the Update method is not that generic as it should be.. you can either pass a type byte[] where the main content has to be wrapped in the name of a collection (failed and static by a previous design) OR it tries to cast a type of map[string]interface{}. Mainly bson.M is only an "alias" for map[string]interface{}, but mongodm wouldn`t detect it.. Can you try the following?

err, _ = reservation.Update(map[string]interface{}{
		"IsSharedRoom":      r.IsSharedRoom,
		"Beds":              r.Beds,
		"PartySize":         r.PartySize,
		"notes":             r.Notes,
		"ConfirmedGuest":    r.ConfirmedGuest,
		"RoomType":          r.RoomType,
		"CheckinDate":       r.CheckinDate,
		"CheckoutDate":      r.CheckoutDate,
		"ReservationNumber": r.ReservationNumber,
	})

And dont forget to call Save() afterwards - this method does not persist anything.

All in all:

  • the Update method is not very good implemented (shame on me)
  • there is missing an else for this type condition check (this is why you should get only nil, nil with your snippet)
  • there is missing the whole documentation and an example for this

Definitely something to fix, thanks!

@moehlone moehlone self-assigned this Apr 18, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants