N
Nuxt4mo ago
high_on_js

Server side pages without server directory for api calls

Can we use pinia store methods server side
5 Replies
Cue
Cue4mo ago
Yes
high_on_js
high_on_jsOP4mo ago
How I am getting cannot stringify non POJO @cuebit I did use useasyncData to call server methodds (api calls ) on my page which is not giving me an error but my page source doesn't contain the data
Cue
Cue4mo ago
Do you have anything to show to give us indication of what the issue could be? Maybe a minimal repro on https://nuxt.new
Lars.
Lars.4mo ago
From a .vue page I use this api from a method in a pinia store and it runs server side:
export default defineEventHandler(async (event) => {
const { auServername, auDatabase, auUsername, auPassword } =
useRuntimeConfig();

if (!auServername || !auDatabase || !auUsername || !auPassword) {
console.error(
"/api/getToken: FileMaker environment variables are not set."
);
throw createError({
statusCode: 500,
message: "FileMaker environment variables are not set.",
});
}
const url = `https://${auServername}/fmi/data/v1/databases/${auDatabase}/sessions`;

async function fetchData(fetchFn) {
try {
const response = await unsecureFetch(url, {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Basic ${btoa(`${auUsername}:${auPassword}`)}`,
},
timeout: 5000,
});

if (process.server) {
console.log("Running server side"); // 'Running server side'
}
if (process.client) {
console.log("Running client side");
}

return {
status: "success",
code: 200,
message: "Data fetched successfully",
data: response,
};
}
}
...
export default defineEventHandler(async (event) => {
const { auServername, auDatabase, auUsername, auPassword } =
useRuntimeConfig();

if (!auServername || !auDatabase || !auUsername || !auPassword) {
console.error(
"/api/getToken: FileMaker environment variables are not set."
);
throw createError({
statusCode: 500,
message: "FileMaker environment variables are not set.",
});
}
const url = `https://${auServername}/fmi/data/v1/databases/${auDatabase}/sessions`;

async function fetchData(fetchFn) {
try {
const response = await unsecureFetch(url, {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Basic ${btoa(`${auUsername}:${auPassword}`)}`,
},
timeout: 5000,
});

if (process.server) {
console.log("Running server side"); // 'Running server side'
}
if (process.client) {
console.log("Running client side");
}

return {
status: "success",
code: 200,
message: "Data fetched successfully",
data: response,
};
}
}
...
I'm using unsecureFetch (based on ofetch) because $fetch uses a default Agent that can't be overridden to ignore a certificate. However, this isn't the core issue here but it shows that Pinia methods can run server side. The problem seems to lie in how useAsyncData is being used. Hope this helps a bit.
high_on_js
high_on_jsOP4mo ago
Thanks for this will try
Want results from more Discord servers?
Add your server