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

Invalid JSON being returned for .Delete(db.DeleteOpts{ReturnChanges: true}) #441

Open
cstetson opened this issue May 23, 2018 · 2 comments
Labels

Comments

@cstetson
Copy link

cstetson commented May 23, 2018

I am working with a microservice application where I need to get back the values for a record that I delete and using the "ReturnChanges" feature of rethink is perfect for this. It works for update and replace, but fails for delete.

resp, err = db.DB("content").Table("posts").Get(articleId).Delete(db.DeleteOpts{ReturnChanges: true}).Run(env.Session)
	if err != nil {
		fmt.Print(err)
		return StatusError{500, err}
	}

	result , _ := resp.NextResponse()
	spew.Dump(result)

	deRefedJson := (*json.RawMessage)(&result)
	spew.Dump(result)

	var jsonResult interface{}
	err = json.Unmarshal(*deRefedJson, &jsonResult)
	spew.Dump(jsonResult)
	var changes Changes
	changes = jsonResult.(Changes)

The return values for the changes come as

{
 "changes": {
  {
   "new_val": null,
   "old_val": {
    "author": "Christopher Stetson",
    "date": {
     "$reql_type$": "TIME",
     "epoch_time": 1.527033878861e+09,
     "timezone": "+00:00"
    },
    "location": "Oakland CA",
    "title": "Test Post 1.4",
    "album_id": 38,
    "body": " This is the body",
    "extract": "This is the extract",
    "id": "b17caf88-c449-4e2a-8fee-f0e498f3d91c",
    "photo": "http://fake-s3:4569/mra-images/uploads/photos/8abe3ad9-50d1-4c91-bbb7-6ca27fbad24c/medium.jpg"
   }
  }
 },
 "deleted": 1,
 "errors": 0,
 "inserted": 0,
 "replaced": 0,
 "skipped": 0,
 "unchanged": 0
}

Where it should be returning the changes as an array:

 "changes": [
  {
   "new_val": null,
   "old_val": {
    "author": "Christopher Stetson",
    "date": {
     "$reql_type$": "TIME",
     "epoch_time": 1.527033878861e+09,
     "timezone": "+00:00"
    },
    "location": "Oakland CA",
    "title": "Test Post 1.4",
    "album_id": 38,
    "body": " This is the body",
    "extract": "This is the extract",
    "id": "b17caf88-c449-4e2a-8fee-f0e498f3d91c",
    "photo": "http://fake-s3:4569/mra-images/uploads/photos/8abe3ad9-50d1-4c91-bbb7-6ca27fbad24c/medium.jpg"
   }
  }
],
 "deleted": 1,
 "errors": 0,
 "inserted": 0,
 "replaced": 0,
 "skipped": 0,
 "unchanged": 0
}

Rethink returns the proper json structure.

@pohzipohzi
Copy link
Contributor

Hi @cstetson. You should be using RunWrite instead of Run for write operations (see this). This will return a WriteResponse struct where you can simply access its Changes property. So it may look something like this:

    resp, _ = db.DB("content").Table("posts").Get(articleId).Delete(r.DeleteOpts{ReturnChanges: true}).RunWrite(session)
    fmt.Printf("%+v\n", resp)
    fmt.Println(reflect.TypeOf(resp.Changes)) // []gorethink.ChangeResponse

@CMogilko CMogilko added the p:low label Aug 22, 2018
@CMogilko
Copy link
Member

@pohzipohzi suggested the right way to get changes, but why json unmarshals to a single struct is needed to be researched.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants