Closed Vahanerevan closed 2 years ago
Hi, @Vahanerevan, thank you for reporting this issue. Do you want to open a PR?
Sure I did not go deep in code so There are 2 ways to fix this issue
1. Idk if there can be case: where id will not be string . Should we return error ?
if _, ok := doc.Get(objectIdField).(string); !ok {
return "",err
}
2.
func (db *DB) InsertOne(collectionName string, doc *Document) (string, error) {
err := db.Insert(collectionName, doc)
if doc.Get(objectIdField) == nil {
return "",err
}
return doc.Get(objectIdField).(string), err
}
Just let me know which one will fit here . I will open PR.
Thank you.
I think the cleanest way is to use the ObjectId()
method of the document struct, which returns an empty string in the case the objectIdField is not set:
func (db *DB) InsertOne(collectionName string, doc *Document) (string, error) {
err := db.Insert(collectionName, doc)
return doc.ObjectId(), err
}
@Vahanerevan, any update on this?
Issue happens when json marshalling document items cause error, in that case the doc id is nil and next line doc.Get(objectIdField).(string) cause error .
//Should be something like