我一直在修修补补与NextJS的一点现在,并试图转换一个应用程序,以预渲染静态页面使用getStaticProps
和getStaticPaths
与[id].js文件,为每个页面在自己的单独文件夹(类似于pages/posts/[id].js,/pages/articles/[id].js等)。由于这两个函数保证在服务器上运行,我选择将fetch db函数从我的公共API直接移动到这些函数。因此在我的[id].js
文件中,我有这样的东西:
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
}
}
型
当我使用npm run build构建应用程序时,我得到的全部是Connecting to Snowflake...
和Found paths: []
,然后显然getStaticProps
中断,因为列表是空的。我不确定为什么无法建立连接,或者为什么我甚至没有得到错误或成功输出。我假设Snowflake如何异步连接有一些问题,但我不明白为什么“await”关键字在这种情况下什么也不做。我可以添加或删除“await”,结果是完全相同的。
2条答案
按热度按时间tjjdgumg1#
正在尝试连接的用户。请确保您的帐户标识符(即组织名称)在与".“之间使用"-”。从snowflake复制的帐户字符串使用“.”。但它不起作用,它必须是-
cigdeys32#
NextJS依赖于NodeJS,因此遵循Snowflake documentation的步骤,我从第一次尝试就开始工作了。
这是我的剧本:
字符串
运行这个脚本,我得到:
型
试试同样的,看看是否有效。