Additionally to my answer here, I may suggest the following workflow with less work with "Virtual Layer".
Let's assume there are two point layers "poi"
(pink) and "end"
(dark green), and one polygon layer "poly"
(blue) with its corresponding attribute tables accordingly, see image below.
Step 1. Deploy a "Virtual Layer" through Layer > Add Layer > Add/Edit Virtual Layer...
with the following query to create all possible lines connected between two points layers.
SELECT "poi".id AS pfrom,"end".id AS pto,
make_line("poi".geometry, "end".geometry) AS geom
FROM "poi", "end"
As an alternative to create all possible connections between points from two layesr you may try using the solution provided in this answer.
Step 2. Proceed with "Extract by location"
with ticked within
geometric predicate.
Note that the algorithm will be applied for lines that are entirely within each single polygon, i.e. no connections with points from neighbor polygons, see image below.
Step 3. Use "Extract by expression"
with the following expression
$length = minimum($length, "pfrom")
. Afterwards the application of "Join attributes by location"
might be relevant.
SELECT ct.name AS city, c.CNTR_CODE AS conflict, cnt.NUTS_NAME AS country, make_line(ct.geometry, c.geometry), ROUND(MIN(ST_Distance(ct.geometry, c.geometry)),1) AS distance, ct.id || '_' || c.id AS uniqueid FROM Sorting_Plants_2020 AS ct, Centroids AS c, National_BC AS cnt WHERE st_within(c.geometry, cnt.geometry) AND st_within(ct.geometry, cnt.geometry) GROUP BY ct.name ORDER BY distance DESC
And when I test this I results in no error..