Error passing MongoDB ObjectID back to .Net Core 2.1 Controller
-
So I can add and delete Mongo documents but I ran into trouble with updates using HTTPPut. When I get the record from the controller, the ObjectId in Angular is sort of truncated down to the string. When I pass it back to the controller, the controller complains that it's not a valid MongoDB ObjectID and HTTP 400 me. The response was not a valid ObjectID. I tried changing my angular model Id to object
public Id: string --- Before
public Id: object --- AfterTook the BSON decorators out of the .Net Model, which is the MongoDB model
[BsonId]
[BsonRepresentation(BsonType.ObjectId)]
public ObjectId Id { get; set; }Not really sure what direction to go here to keep my mean stack. HtTP Response
HTTP/1.1 400 Bad Request
Transfer-Encoding: chunked
Content-Type: application/json; charset=utf-8
Vary: Origin
Server: Kestrel
Access-Control-Allow-Credentials: true
Access-Control-Allow-Origin: https://localhost:44367
X-Frame-Options: DENY
X-SourceFiles: =?UTF-8?B?RzpcamtpcmtlcnhBbmd1bGFyXGFwaVxwb3J0Zm9saW9zXFVwZGF0ZVBvcnRmb2xpb1w1YmFkNTEyMGM0MzFjYjA1NjQxNTE2NTk=?=
X-Powered-By: ASP.NET
Date: Thu, 27 Sep 2018 23:09:47 GMT7f
{"Id":["Error converting value \"5bad5120c431cb0564151659\" to type 'MongoDB.Bson.ObjectId'. Path 'Id', line 1, position 32."]}
0If it ain't broke don't fix it Discover my world at jkirkerx.com
-
So I can add and delete Mongo documents but I ran into trouble with updates using HTTPPut. When I get the record from the controller, the ObjectId in Angular is sort of truncated down to the string. When I pass it back to the controller, the controller complains that it's not a valid MongoDB ObjectID and HTTP 400 me. The response was not a valid ObjectID. I tried changing my angular model Id to object
public Id: string --- Before
public Id: object --- AfterTook the BSON decorators out of the .Net Model, which is the MongoDB model
[BsonId]
[BsonRepresentation(BsonType.ObjectId)]
public ObjectId Id { get; set; }Not really sure what direction to go here to keep my mean stack. HtTP Response
HTTP/1.1 400 Bad Request
Transfer-Encoding: chunked
Content-Type: application/json; charset=utf-8
Vary: Origin
Server: Kestrel
Access-Control-Allow-Credentials: true
Access-Control-Allow-Origin: https://localhost:44367
X-Frame-Options: DENY
X-SourceFiles: =?UTF-8?B?RzpcamtpcmtlcnhBbmd1bGFyXGFwaVxwb3J0Zm9saW9zXFVwZGF0ZVBvcnRmb2xpb1w1YmFkNTEyMGM0MzFjYjA1NjQxNTE2NTk=?=
X-Powered-By: ASP.NET
Date: Thu, 27 Sep 2018 23:09:47 GMT7f
{"Id":["Error converting value \"5bad5120c431cb0564151659\" to type 'MongoDB.Bson.ObjectId'. Path 'Id', line 1, position 32."]}
0If it ain't broke don't fix it Discover my world at jkirkerx.com
It looks like a fairly standard serialization error. I don't think you need the BsonRepresentationAttribute here. Have you had a look at the docs: [Mapping Classes](http://mongodb.github.io/mongo-csharp-driver/2.0/reference/bson/mapping/#the-id-member) The default for Id mapping is the string type, but you can use ObjectId as you are, but you need to set a specific IdGenerator. Going through the references for the various IdGenerators in the MongoDB.Bson.Serialization.IdGenerators namespace, it looks like there are 3 candidate generators for ObjectId. I don't know which one is more appropriate for your use case, but if I were to venture a guess the general ObjectIdGenerator should be appropriate: [MongoDB.Bson.Serialization.IdGenerators Namespace](http://api.mongodb.com/csharp/2.0/html/N\_MongoDB\_Bson\_Serialization\_IdGenerators.htm) So, your mapping ultimately should look like:
[BsonId(IdGenerator = typeof(ObjectIdGenerator)]
public ObjectId Id { get; set; }"Never attribute to malice that which can be explained by stupidity." - Hanlon's Razor
-
It looks like a fairly standard serialization error. I don't think you need the BsonRepresentationAttribute here. Have you had a look at the docs: [Mapping Classes](http://mongodb.github.io/mongo-csharp-driver/2.0/reference/bson/mapping/#the-id-member) The default for Id mapping is the string type, but you can use ObjectId as you are, but you need to set a specific IdGenerator. Going through the references for the various IdGenerators in the MongoDB.Bson.Serialization.IdGenerators namespace, it looks like there are 3 candidate generators for ObjectId. I don't know which one is more appropriate for your use case, but if I were to venture a guess the general ObjectIdGenerator should be appropriate: [MongoDB.Bson.Serialization.IdGenerators Namespace](http://api.mongodb.com/csharp/2.0/html/N\_MongoDB\_Bson\_Serialization\_IdGenerators.htm) So, your mapping ultimately should look like:
[BsonId(IdGenerator = typeof(ObjectIdGenerator)]
public ObjectId Id { get; set; }"Never attribute to malice that which can be explained by stupidity." - Hanlon's Razor
I thought about it last night and decided to just remove that column and not use the BSON Object ID, but to use the string version of the Object ID instead. This might get me later on when I find out that I need that Object for something, not sure what yet, To lookup the server that wrote the record or the other data stored in the ObjectId. After testing just now, I can finally reach the .Net Controller for Update without my posted error. Well basically my mean stack now works for updates using HTTPPUT. First time so I'm excited because it completed my stack operations. But I'll go ahead and investigate and try that type and see what happens. I thought maybe I needed to decorate the controller action with something that would regenerate the ObjectId back to the proper format. Thanks for helping me!
If it ain't broke don't fix it Discover my world at jkirkerx.com
-
It looks like a fairly standard serialization error. I don't think you need the BsonRepresentationAttribute here. Have you had a look at the docs: [Mapping Classes](http://mongodb.github.io/mongo-csharp-driver/2.0/reference/bson/mapping/#the-id-member) The default for Id mapping is the string type, but you can use ObjectId as you are, but you need to set a specific IdGenerator. Going through the references for the various IdGenerators in the MongoDB.Bson.Serialization.IdGenerators namespace, it looks like there are 3 candidate generators for ObjectId. I don't know which one is more appropriate for your use case, but if I were to venture a guess the general ObjectIdGenerator should be appropriate: [MongoDB.Bson.Serialization.IdGenerators Namespace](http://api.mongodb.com/csharp/2.0/html/N\_MongoDB\_Bson\_Serialization\_IdGenerators.htm) So, your mapping ultimately should look like:
[BsonId(IdGenerator = typeof(ObjectIdGenerator)]
public ObjectId Id { get; set; }"Never attribute to malice that which can be explained by stupidity." - Hanlon's Razor
Do I really need to store that Object ID in it's natural format? Or is it possible to just store the string, and then convert it back to it's natural format using something on the .Net Core side if I need it? Maybe on the Angular side there is something to convert it with as well? My mean stack fully works now, guess I'll try your research and see what happens. If it works I'll store the Object. This is how my Mongo record looks now.
{
"_id" : "5bae68208301500628d80f8c",
"TimeStamp" : ISODate("2018-09-28T17:42:57.068Z"),
"Name" : "Long Beach Grand Prix 2008",
"Description" : "I took this photo at the Long Beach Grand Prix",
"CreatedBy" : "admin",
"HTML" : "",
"AvatarB64" : "data:image/jpeg;base64, data string.
"AvatarName" : "5bae68208301500628d80f8c.jpg",
"AvatarType" : "image/jpeg",
"AvatarUrl" : "https://localhost:44367/content/images/avatars/portfolios/5bae68208301500628d80f8c.jpg",
"AvatarX" : 256,
"AvatarY" : 256,
"AvatarStatus" : true
}If it ain't broke don't fix it Discover my world at jkirkerx.com
-
Do I really need to store that Object ID in it's natural format? Or is it possible to just store the string, and then convert it back to it's natural format using something on the .Net Core side if I need it? Maybe on the Angular side there is something to convert it with as well? My mean stack fully works now, guess I'll try your research and see what happens. If it works I'll store the Object. This is how my Mongo record looks now.
{
"_id" : "5bae68208301500628d80f8c",
"TimeStamp" : ISODate("2018-09-28T17:42:57.068Z"),
"Name" : "Long Beach Grand Prix 2008",
"Description" : "I took this photo at the Long Beach Grand Prix",
"CreatedBy" : "admin",
"HTML" : "",
"AvatarB64" : "data:image/jpeg;base64, data string.
"AvatarName" : "5bae68208301500628d80f8c.jpg",
"AvatarType" : "image/jpeg",
"AvatarUrl" : "https://localhost:44367/content/images/avatars/portfolios/5bae68208301500628d80f8c.jpg",
"AvatarX" : 256,
"AvatarY" : 256,
"AvatarStatus" : true
}If it ain't broke don't fix it Discover my world at jkirkerx.com
I've always just modeled it as a string when I've needed to. I assumed you were going for the C# Mongo ObjectId for your own reasons :p Keep in mind, this isn't about how you're storing it, but rather just how you're referencing it after deserialization.
"Never attribute to malice that which can be explained by stupidity." - Hanlon's Razor
-
So I can add and delete Mongo documents but I ran into trouble with updates using HTTPPut. When I get the record from the controller, the ObjectId in Angular is sort of truncated down to the string. When I pass it back to the controller, the controller complains that it's not a valid MongoDB ObjectID and HTTP 400 me. The response was not a valid ObjectID. I tried changing my angular model Id to object
public Id: string --- Before
public Id: object --- AfterTook the BSON decorators out of the .Net Model, which is the MongoDB model
[BsonId]
[BsonRepresentation(BsonType.ObjectId)]
public ObjectId Id { get; set; }Not really sure what direction to go here to keep my mean stack. HtTP Response
HTTP/1.1 400 Bad Request
Transfer-Encoding: chunked
Content-Type: application/json; charset=utf-8
Vary: Origin
Server: Kestrel
Access-Control-Allow-Credentials: true
Access-Control-Allow-Origin: https://localhost:44367
X-Frame-Options: DENY
X-SourceFiles: =?UTF-8?B?RzpcamtpcmtlcnhBbmd1bGFyXGFwaVxwb3J0Zm9saW9zXFVwZGF0ZVBvcnRmb2xpb1w1YmFkNTEyMGM0MzFjYjA1NjQxNTE2NTk=?=
X-Powered-By: ASP.NET
Date: Thu, 27 Sep 2018 23:09:47 GMT7f
{"Id":["Error converting value \"5bad5120c431cb0564151659\" to type 'MongoDB.Bson.ObjectId'. Path 'Id', line 1, position 32."]}
0If it ain't broke don't fix it Discover my world at jkirkerx.com
jkirkerx wrote:
ObjectId....HTTP...application/json
You have an object. You are attempting to serialize it to json for use in an http request. I suspect you might want deserialize at some point as well. So you need a json serializer/deserializer. Just as you would for any object. It probably already is doing that but what you are getting is the complex form (the Mongodb form fully blown out) and what you actually want is the stringified version of that id. So you need a custom serializer/deserializer. You should already have an http layer is some form so you just need to add that in there.
-
I've always just modeled it as a string when I've needed to. I assumed you were going for the C# Mongo ObjectId for your own reasons :p Keep in mind, this isn't about how you're storing it, but rather just how you're referencing it after deserialization.
"Never attribute to malice that which can be explained by stupidity." - Hanlon's Razor
nah, just no clue and part of the learning process.
If it ain't broke don't fix it Discover my world at jkirkerx.com
-
jkirkerx wrote:
ObjectId....HTTP...application/json
You have an object. You are attempting to serialize it to json for use in an http request. I suspect you might want deserialize at some point as well. So you need a json serializer/deserializer. Just as you would for any object. It probably already is doing that but what you are getting is the complex form (the Mongodb form fully blown out) and what you actually want is the stringified version of that id. So you need a custom serializer/deserializer. You should already have an http layer is some form so you just need to add that in there.
I saw some examples of serializing and deserializing the ObjectId. But wanted to see some progress or get my mean stack working first. In the database I stored the #C .Net ObjectId, but say on a Http Get request Angular got it as a simple string. Send it from Angular back to the controller with a Put or Post sent the string, but the controller was looking for the ObjectId. I get it now, since I took the ObjectId out of the model and can Get, Post, Put, Delete now. I'll take your thoughts and research more on it, do some test. Overall: This Portfolio module I just wrote in Angular 6 TypeScript is pretty slick. Drag and Drop, Select image button and extract it as a base64 string and update the thumbnail preview. You can change the image as many times as you want, I don't care. Then submit and fire off the model to the controller, controller processes the image, even flips it around if it's from an iPhone, write it's to disk, creates a MongoDB document, then updates the model and sends it back to Angular and updates the page. I just need a pretty progress icon now. I wrote very few lines of code, mostly worked on keeping really small and compact and tons of research and experimenting. 730 lines. I should measure the image data and store it in the database to detect image updates, and then fire off the graphics mode.
If it ain't broke don't fix it Discover my world at jkirkerx.com
-
So I can add and delete Mongo documents but I ran into trouble with updates using HTTPPut. When I get the record from the controller, the ObjectId in Angular is sort of truncated down to the string. When I pass it back to the controller, the controller complains that it's not a valid MongoDB ObjectID and HTTP 400 me. The response was not a valid ObjectID. I tried changing my angular model Id to object
public Id: string --- Before
public Id: object --- AfterTook the BSON decorators out of the .Net Model, which is the MongoDB model
[BsonId]
[BsonRepresentation(BsonType.ObjectId)]
public ObjectId Id { get; set; }Not really sure what direction to go here to keep my mean stack. HtTP Response
HTTP/1.1 400 Bad Request
Transfer-Encoding: chunked
Content-Type: application/json; charset=utf-8
Vary: Origin
Server: Kestrel
Access-Control-Allow-Credentials: true
Access-Control-Allow-Origin: https://localhost:44367
X-Frame-Options: DENY
X-SourceFiles: =?UTF-8?B?RzpcamtpcmtlcnhBbmd1bGFyXGFwaVxwb3J0Zm9saW9zXFVwZGF0ZVBvcnRmb2xpb1w1YmFkNTEyMGM0MzFjYjA1NjQxNTE2NTk=?=
X-Powered-By: ASP.NET
Date: Thu, 27 Sep 2018 23:09:47 GMT7f
{"Id":["Error converting value \"5bad5120c431cb0564151659\" to type 'MongoDB.Bson.ObjectId'. Path 'Id', line 1, position 32."]}
0If it ain't broke don't fix it Discover my world at jkirkerx.com
I'm having the same problem as you, happy wheels free