So I have an MEAN stack application where user types query ( or copy from mongodb compass ) in a editor and I use nodejs in backend to perform that query ,
So I use codemirror editor to text codes , currently I enabled javascript editor to allow users to type ObjectId , as sometimes user type query in Mongodb compass and paste here in input so how can I safely convert the js object to mongodb query
initJsonEditor() {
if (this.jsonEditorView) {
this.jsonEditorView.destroy();
}
let editorTarget: ElementRef | null = null;
let form: FormGroup | null = null;
if (this.selected_widget_type === DASHBOARD_CONSTANTS.html_content) {
editorTarget = this.htmlQueryEditor;
form = this.html_form;
} else if (this.selected_widget_type === DASHBOARD_CONSTANTS.chart) {
editorTarget = this.chartQueryEditor;
form = this.chart_form;
} else if (this.selected_widget_type === DASHBOARD_CONSTANTS.table) {
editorTarget = this.tableQueryEditor;
form = this.table_form;
}
if (!editorTarget?.nativeElement) {
setTimeout(() => this.initJsonEditor(), 50);
return;
}
if (!editorTarget || !form) return;
this.jsonEditorErrorMessage = null;
this.jsonEditorView = new EditorView({
parent: editorTarget.nativeElement,
state: EditorState.create({
doc: form.get("query")?.value || "",
extensions: [
basicSetup,
oneDark,
javascript({
jsx : false,
typescript : false,
}),
lintGutter(),
// this.jsonLint(),
EditorView.updateListener.of((update) => {
if (update.docChanged) {
form.get("query")?.setValue(update.state.doc.toString());
}
}),
],
}),
});
}
Above code ifor my editor for codemirror as in js
And while submitting form I checking that parse as it's valid
const fn = new Function (
const ObjectId = (id) => ( { $oid : id });
return query
)
The above code is works but if I type { $oid then I get error as I wrap that code in try catch so , is there any way that I can parse safely and correctly