I've been tinkering around with NextJS for a bit now and am trying to convert an app to prerender static pages using getStaticProps
and getStaticPaths
with [id].js files for each page in their own separate folders (somthing like pages/posts/[id].js, /pages/articles/[id].js, etc.). Since those two functions are guaranteed to run on the server, I opted to move the fetch db functions from my public API to those functions directly. So within my [id].js
files, I have something like this:
getStaticProps:
export async function getStaticProps({ params }) {
let snowflake = require('snowflake-sdk')
require('dotenv').config();
let result = {};
let id = params.id;
const connection = snowflake.createConnection( {
account: "account",
username: "username",
password: "password",
warehouse: "warehouse"
});
console.log("Connecting to Snowflake...")
await connection.connect( <- await doesn't seem to make a difference
function(err, conn) {
if (err) {
console.error('Unable to connect: ' + err.message); <- This never outputs
}
else {
console.log('Successfully connected to Snowflake.'); <- Neither does this
}
}
);
await connection.execute({ <- await doesn't seem to make a difference
sqlText: (`select stuff
from my_snowflake_db
where id=:1`),
binds: [id],
complete: function(err, stmt, data) {
if (err) {
console.error('Failed to execute statement due to the following error: ' + err.message);
} else {
result = data
result["id"] = id
}
}
})
return {
props: {
result
}
}
}
getStaticPaths:
export async function getStaticPaths() {
let snowflake = require('snowflake-sdk')
require('dotenv').config();
let paths = []
const connection = snowflake.createConnection( {
account: "account",
username: "username",
password: "password",
warehouse: "warehouse"
});
console.log("Connecting to Snowflake...")
await connection.connect( <- await doesn't seem to make a difference
function(err, conn) {
if (err) {
console.error('Unable to connect: ' + err.message); <- This never outputs
}
else {
console.log('Successfully connected to Snowflake.'); <- Neither does this
}
}
);
await connection.execute({ <- await doesn't seem to make a difference
sqlText: (`select id
from my_snowflake_db
where ...
`),
complete: function(err, stmt, rows) {
if (err) {
console.error('Failed to execute statement due to the following error: ' + err.message); <- Never outputs
} else {
console.log("Got Response!") <- Never outputs
for (var row in rows) {
console.log("Found id: " + row.id)
paths.push({
params: {
id: row.id
}
})
}
}
}
})
console.log("Found paths: " + JSON.stringify(paths)) <- This outputs with an empty list
return {
paths,
fallback: true
}
}
When I build the app with npm run build, all I get are Connecting to Snowflake...
and Found paths: []
, then obviously getStaticProps
breaks since the list is empty. I'm not sure why the connection can't be established, or why I'm not even getting error or success output. I'm assuming there's some issue with how Snowflake connects asynchronously, but I don't understand why the "await" keyword does nothing in this case. I can add or remove "await" and the result is the exact same.
NextJS depends on NodeJS therefore following the steps from Snowflake documentation I got it working from first attempt.
This is my script:
Running this script I get:
Try same and see if that works.