@@ -198,16 +198,27 @@ async function alterHistoryNext(objToUpdate, newNextID) {
198198async function getAllVersions ( obj ) {
199199 let ls_versions
200200 let primeID = obj ?. __rerum . history . prime
201- let rootObj = ( primeID === "root" )
202- ? //The obj passed in is root. So it is the rootObj we need.
203- JSON . parse ( JSON . stringify ( obj ) )
204- : //The obj passed in knows the ID of root, grab it from Mongo
205- await db . findOne ( { "@id" : primeID } )
206- /**
207- * Note that if you attempt the following code, it will cause Cannot convert undefined or null to object in getAllVersions.
208- * rootObj = await db.findOne({"$or":[{"_id": primeID}, {"__rerum.slug": primeID}]})
209- * This is the because some of the @ids have different RERUM URL patterns on them.
210- **/
201+ let rootObj
202+ if ( primeID === "root" ) {
203+ //The obj passed in is root. So it is the rootObj we need.
204+ rootObj = JSON . parse ( JSON . stringify ( obj ) )
205+ } else if ( primeID ) {
206+ //The obj passed in knows the ID of root, grab it from Mongo
207+ //Use _id for indexed query performance instead of @id
208+ let primeHexId
209+ try {
210+ primeHexId = parseDocumentID ( primeID )
211+ } catch ( error ) {
212+ throw new Error ( `Invalid history.prime value '${ primeID } ': ${ error . message } ` )
213+ }
214+ rootObj = await db . findOne ( { "$or" :[ { "_id" : primeHexId } , { "__rerum.slug" : primeHexId } ] } )
215+ if ( ! rootObj ) {
216+ throw new Error ( `Root object with id '${ primeID } ' not found in database` )
217+ }
218+ } else {
219+ //primeID is undefined or null, cannot proceed
220+ throw new Error ( "Object has no valid history.prime value" )
221+ }
211222 //All the children of this object will have its @id in __rerum.history.prime
212223 ls_versions = await db . find ( { "__rerum.history.prime" : rootObj [ '@id' ] } ) . toArray ( )
213224 //The root object is a version, prepend it in
0 commit comments