Skip to content

Commit bf723f6

Browse files
Copilotrobfrank
andcommitted
Fix MongoDB queries in Studio by using correct API endpoint
- Change executeCommandTable() and executeCommandGraph() to use /api/v1/query/ endpoint for MongoDB queries instead of /api/v1/command/ - Add defensive null checks for data.result to prevent JavaScript errors when response is empty - MongoDB queries now work correctly in Studio as the /query endpoint uses database.query() which is implemented for MongoDB Co-authored-by: robfrank <413587+robfrank@users.noreply.github.com>
1 parent 58938a0 commit bf723f6

File tree

1 file changed

+20
-5
lines changed

1 file changed

+20
-5
lines changed

studio/src/main/resources/static/js/studio-database.js

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -561,10 +561,13 @@ function executeCommandTable() {
561561

562562
let beginTime = new Date();
563563

564+
// Use /query endpoint for mongo queries, /command for others
565+
let endpoint = language === "mongo" ? "api/v1/query/" + database : "api/v1/command/" + database;
566+
564567
jQuery
565568
.ajax({
566569
type: "POST",
567-
url: "api/v1/command/" + database,
570+
url: endpoint,
568571
data: JSON.stringify({
569572
language: language,
570573
command: command,
@@ -580,7 +583,11 @@ function executeCommandTable() {
580583
let elapsed = new Date() - beginTime;
581584
$("#result-elapsed").html(elapsed);
582585

583-
$("#result-num").html(data.result.records.length);
586+
if (data && data.result && data.result.records) {
587+
$("#result-num").html(data.result.records.length);
588+
} else {
589+
$("#result-num").html(0);
590+
}
584591
$("#resultJson").val(JSON.stringify(data, null, 2));
585592
$("#resultExplain").val(data.explain != null ? data.explain : "No profiler data found");
586593

@@ -612,10 +619,13 @@ function executeCommandGraph() {
612619

613620
let beginTime = new Date();
614621

622+
// Use /query endpoint for mongo queries, /command for others
623+
let endpoint = language === "mongo" ? "api/v1/query/" + database : "api/v1/command/" + database;
624+
615625
jQuery
616626
.ajax({
617627
type: "POST",
618-
url: "api/v1/command/" + database,
628+
url: endpoint,
619629
data: JSON.stringify({
620630
language: language,
621631
command: command,
@@ -631,7 +641,11 @@ function executeCommandGraph() {
631641
let elapsed = new Date() - beginTime;
632642
$("#result-elapsed").html(elapsed);
633643

634-
$("#result-num").html(data.result.records.length);
644+
if (data && data.result && data.result.records) {
645+
$("#result-num").html(data.result.records.length);
646+
} else {
647+
$("#result-num").html(0);
648+
}
635649
$("#resultJson").val(JSON.stringify(data, null, 2));
636650
$("#resultExplain").val(data.explain != null ? data.explain : "'");
637651

@@ -640,7 +654,8 @@ function executeCommandGraph() {
640654

641655
let activeTab = $("#tabs-command .active").attr("id");
642656

643-
if (data.result.vertices.length == 0 && data.result.records.length > 0) {
657+
if (data && data.result && data.result.vertices && data.result.records &&
658+
data.result.vertices.length == 0 && data.result.records.length > 0) {
644659
if (activeTab == "tab-table-sel") renderTable();
645660
else globalActivateTab("tab-table");
646661
} else {

0 commit comments

Comments
 (0)