{"record":{"id":"2e55d138a063a589","repo":"dgraph-io/dgraph","slug":"value-of-to-var-s-should-have-already-been-popul","errorCode":null,"errorMessage":"value of to var(%s) should have already been populated","messagePattern":"value of to var\\((.+?)\\) should have already been populated","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"query/query.go","lineNumber":1791,"sourceCode":"\t\tfromVar := sg.Params.ShortestPathArgs.From.NeedsVar[0].Name\n\t\tuidVar, ok := mp[fromVar]\n\t\tif !ok {\n\t\t\treturn errors.Errorf(\"value of from var(%s) should have already been populated\",\n\t\t\t\tfromVar)\n\t\t}\n\t\tif uidVar.Uids != nil && len(uidVar.Uids.Uids) > 0 {\n\t\t\tif len(uidVar.Uids.Uids) > 1 {\n\t\t\t\treturn errors.Errorf(\"from variable(%s) should only expand to 1 uid\", fromVar)\n\t\t\t}\n\t\t\tsg.Params.From = uidVar.Uids.Uids[0]\n\t\t}\n\t}\n\n\tif sg.Params.ShortestPathArgs.To != nil && len(sg.Params.ShortestPathArgs.To.NeedsVar) > 0 {\n\t\ttoVar := sg.Params.ShortestPathArgs.To.NeedsVar[0].Name\n\t\tuidVar, ok := mp[toVar]\n\t\tif !ok {\n\t\t\treturn errors.Errorf(\"value of to var(%s) should have already been populated\",\n\t\t\t\ttoVar)\n\t\t}\n\t\tif uidVar.Uids != nil && len(uidVar.Uids.Uids) > 0 {\n\t\t\tif len(uidVar.Uids.Uids) > 1 {\n\t\t\t\treturn errors.Errorf(\"to variable(%s) should only expand to 1 uid\", toVar)\n\t\t\t}\n\t\t\tsg.Params.To = uidVar.Uids.Uids[0]\n\t\t}\n\t}\n\treturn nil\n}\n\n// fillVars reads the value corresponding to a variable from the map mp and stores it inside\n// SubGraph. This value is then later used for execution of the SubGraph.\nfunc (sg *SubGraph) fillVars(mp map[string]varValue) error {\n\tif sg.Params.Alias == \"shortest\" {\n\t\tif err := sg.fillShortestPathVars(mp); err != nil {\n\t\t\treturn err","sourceCodeStart":1773,"sourceCodeEnd":1809,"githubUrl":"https://github.com/dgraph-io/dgraph/blob/759e242be62c91f8d084da06ad0c8d21256d9c07/query/query.go#L1773-L1809","documentation":"Analogous to the from-var case: for shortest-path queries the 'to' endpoint supplied via a variable must already exist in the computed variables map. If the named variable is missing from mp, Dgraph throws this error because the destination was never populated.","triggerScenarios":"shortest(..., to: var(t)) where t was never defined with 'as', was defined later than the shortest-path block, or its emitting clause matched nothing so it never entered the done-vars map.","commonSituations":"Typo in the to-variable name; variable emitted only inside a conditional branch; query restructured so the shortest-path block executes before the variable is defined.","solutions":["Define the to variable with 'as' in the same query block before consumption","Check spelling of the variable in shortest(to: var(...))","Ensure the emitting clause matches at least one node","Reorder queries so the variable is materialized before the shortest-path subgraph runs"],"exampleFix":"// before: var(tgt) undefined\n{\n  p(func: uid(0x1)) { shortest(to: var(tgt)) { uid } }\n}\n// after\n{\n  tgt as var(func: eq(name, \"bob\"))\n  p(func: uid(0x1)) { shortest(to: var(tgt)) { uid } }\n}","handlingStrategy":"validation","validationCode":"for _, v := range referencedVars(query, \"shortest\", \"to\") {\n    if !definedBefore(query, v) {\n        return fmt.Errorf(\"to-variable %s must be defined before shortest()\", v)\n    }\n}","typeGuard":null,"tryCatchPattern":"if err != nil && strings.Contains(err.Error(), \"value of to var\") {\n    return fmt.Errorf(\"to-variable missing or empty; check its definition: %w\", err)\n}","preventionTips":["Define to-variables before the shortest-path clause","Confirm the emitting clause matches at least one node before running shortest()","Keep variable definitions and consumers in the same query block"],"tags":["dgraph","query","shortest-path","variables"],"backgroundTag":"shortest-path-variable-not-populated","analyzedSha":"759e242be62c91f8d084da06ad0c8d21256d9c07","analyzedAt":"2026-09-01T14:42:12.034Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}