I wrote this script:
import { createClient } from "edgedb";
import e from "./dbschema/edgeql-js/index";
(async function () {
const client = createClient();
const newCharacters = [
{
portrayed_by: "Robert Downey Jr.",
name: "Iron Man",
movies: ["Iron Man", "Iron Man 2", "Iron Man 3"],
},
{
portrayed_by: "Chris Evans",
name: "Captain America",
movies: [
"Captain America: The First Avenger",
"The Avengers",
"Captain America: The Winter Soldier",
],
},
{
portrayed_by: "Mark Ruffalo",
name: "The Hulk",
movies: ["The Avengers", "Iron Man 3", "Avengers: Age of Ultron"],
},
];
const query = e.params({ characters: e.json }, (params) => {
const movies = e.for(
e.op("distinct", params.characters.movies),
(movie) => {
return e
.insert(e.Movie, {
title: e.cast(e.str, movie),
})
.unlessConflict((movie) => ({
on: movie.title,
else: movie,
}));
}
);
// Iterate characters
return e.for(e.json_array_unpack(params.characters), (character) => {
// inserting each one
return e.insert(e.Character, {
name: e.cast(e.str, character.name),
portrayed_by: e.cast(e.str, character.portrayed_by),
//Iterate movies
movies: e.assert_distinct(
e.for(e.json_array_unpack(character.movies), (movieTitle) => {
// Inserting and linking any that are missing
return e.select(movies, () => ({
filter_single: { title: e.cast(e.str, movieTitle) },
}));
})
),
});
});
});
console.log("query:", query.toEdgeQL());
const result = await query.run(client, {
characters: newCharacters,
});
console.log(result);
})();The intent was to add newCharacters to my database, but it doesn’t work. The console output when I run it:
InvalidValueError: cannot index JSON array by text
at RawConnection._parseErrorMessage (/Users/raddevon/Documents/projects/edgedb/troubleshooting/ts-qb-for-test/node_modules/edgedb/dist/baseConn.js:310:21)
at RawConnection._executeFlow (/Users/raddevon/Documents/projects/edgedb/troubleshooting/ts-qb-for-test/node_modules/edgedb/dist/baseConn.js:918:34)
at processTicksAndRejections (node:internal/process/task_queues:96:5)
at RawConnection.fetch (/Users/raddevon/Documents/projects/edgedb/troubleshooting/ts-qb-for-test/node_modules/edgedb/dist/baseConn.js:970:17)
at ClientConnectionHolder.retryingFetch (/Users/raddevon/Documents/projects/edgedb/troubleshooting/ts-qb-for-test/node_modules/edgedb/dist/baseClient.js:178:26)
at Client.query (/Users/raddevon/Documents/projects/edgedb/troubleshooting/ts-qb-for-test/node_modules/edgedb/dist/baseClient.js:487:20)
at null.<anonymous> (/Users/raddevon/Documents/projects/edgedb/troubleshooting/ts-qb-for-test/linked-object-per-nested-array-item.ts:65:18) {
source: undefined
}
This tells me there is a problem and points to the line where I run my query before going cold and tracing into the client binding itself. Unfortunately, this isn’t very helpful. My query is long, and I don’t know what part of the query is the problem which makes it much harder to solve.