pocketbase url #4938
-
is there a way to get the pocketbase instace url in the js hooks so that i can call custom routes const res = $http.send({
url: pb_url+"/custom_utility_bills?curr_year=2023&prev_year=2023&curr_month=10&prev_month=10",
method: "GET",
body: "", // ex. JSON.stringify({"test": 123}) or new FormData()
headers: { "content-type": "application/json" },
timeout: 120, // in seconds
});
without hardcoding the whole URL |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
You can access the configured settings "Application url" via I'm not sure what is your use case but note that if you are using the JSVM you have direct access to the database and sending HTTP requests to a PocketBase route from a hook usually shouldn't be necessary. |
Beta Was this translation helpful? Give feedback.
-
have acustom route hat looks like this routerAdd(
"GET",
"/custom_utility_bills",
(c) => {
try {
function isParamEmpty(param, default_value) {
if (param === "" || param === undefined || param === null) {
return default_value;
}
return param;
}
const result = arrayOf(
new DynamicModel({
shop_id: "",
curr_bill_id: "",
prev_bill_id: "",
shop_number: "",
shop_name: "",
curr_year: "",
curr_month: "",
prev_year: "",
prev_month: "",
list_order: "",
current_elec: "",
previous_elec: "",
elec_diff: "",
current_water: "",
previous_water: "",
water_diff: "",
})
);
$app
?.dao()
?.db()
?.newQuery(
`
SELECT
sh.id as shop_id,
IFNULL(curr.id,"blank") as curr_bill_id,
IFNULL(prev.id,"blank") as prev_bill_id,
sh.shop_number as shop_number,
te.username as shop_name,
sh."order" as list_order,
IFNULL(curr.month,0) as curr_month,
IFNULL(prev.month,0) as prev_month,
IFNULL(curr.year,0) as curr_year,
IFNULL(prev.year,0) as prev_year,
IFNULL(curr.elec_readings,0) as current_elec,
IFNULL(prev.elec_readings,0) as previous_elec,
IFNULL((curr.elec_readings - prev.elec_readings),0) elec_diff,
IFNULL(curr.water_readings,0) as current_water,
IFNULL(prev.water_readings,0) as previous_water,
IFNULL((curr.water_readings - prev.water_readings),0)water_diff
FROM utility_shops sh
LEFT JOIN utility_bills as curr
ON curr.shop = sh.id AND curr.month = {:curr_month} AND curr.year = {:curr_year}
LEFT JOIN utility_bills as prev
ON prev.shop = sh.id AND prev.month = {:prev_month} AND prev.year = {:prev_year}
LEFT JOIN utility_tenants te
ON te.id = sh.tenant
WHERE sh.is_vacant = false
ORDER BY sh."order";
`
)
?.bind({
curr_year: isParamEmpty(c.queryParam("curr_year"),new Date().getFullYear()),
curr_month: isParamEmpty(c.queryParam("curr_month"),new Date().getMonth()),
prev_year: isParamEmpty(c.queryParam("prev_year"),new Date().getFullYear()),
prev_month: isParamEmpty(c.queryParam("prev_month"),new Date().getMonth()-1),
})
?.all(result); // throw an error on db failure
// if (result.length > 0) {
// console.log(result[0]);
// }
return c.json(200, { result });
} catch (e) {
return c.json(500, {
error: ` Error getting custom_utility_bills : ${e.message}`,
});
}
} /* optional middlewares */
); and wanted to reuse it |
Beta Was this translation helpful? Give feedback.
You can access the configured settings "Application url" via
$app.settings().meta.appUrl
.I'm not sure what is your use case but note that if you are using the JSVM you have direct access to the database and sending HTTP requests to a PocketBase route from a hook usually shouldn't be necessary.