Check type is string before match

This commit is contained in:
Mel O'Hagan 2022-05-13 21:22:10 +01:00
parent 672bfda7dd
commit af997be2e9
1 changed files with 4 additions and 4 deletions

View File

@ -85,12 +85,12 @@ module MongoDBModule {
createObjectIds(json: any): object { createObjectIds(json: any): object {
const self = this const self = this
function replaceObjectIds(json: any) { function interpolateObjectIds(json: any) {
for (let field of Object.keys(json)) { for (let field of Object.keys(json)) {
if (json[field] instanceof Object) { if (json[field] instanceof Object) {
json[field] = self.createObjectIds(json[field]) json[field] = self.createObjectIds(json[field])
} }
if (field === "_id") { if (field === "_id" && typeof json[field] === "string") {
const id = json["_id"].match( const id = json["_id"].match(
/(?<=objectid\(['"]).*(?=['"]\))/gi /(?<=objectid\(['"]).*(?=['"]\))/gi
)?.[0] )?.[0]
@ -104,11 +104,11 @@ module MongoDBModule {
if (Array.isArray(json)) { if (Array.isArray(json)) {
for (let i = 0; i < json.length; i++) { for (let i = 0; i < json.length; i++) {
json[i] = replaceObjectIds(json[i]) json[i] = interpolateObjectIds(json[i])
} }
return json return json
} }
return replaceObjectIds(json) return interpolateObjectIds(json)
} }
async create(query: { json: object; extra: { [key: string]: string } }) { async create(query: { json: object; extra: { [key: string]: string } }) {