在 NextJS 中使用 Snowflake 无法连接或执行 getStaticPaths/getStaticProps 中的查询
Posted
技术标签:
【中文标题】在 NextJS 中使用 Snowflake 无法连接或执行 getStaticPaths/getStaticProps 中的查询【英文标题】:Using Snowflake in NextJS can't connect or execute queries in getStaticPaths/getStaticProps 【发布时间】:2021-01-31 11:10:49 【问题描述】:我现在一直在用 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”关键字在这种情况下什么都不做。我可以添加或删除“等待”,结果完全一样。
【问题讨论】:
【参考方案1】:NextJS 依赖于 NodeJS,因此按照 Snowflake documentation 的步骤,我从第一次尝试就开始工作了。
这是我的脚本:
async function connectToSnowflake()
let snowflake = require('snowflake-sdk');
var connection = snowflake.createConnection(
account: 'XXXXX',
username: 'XXXXX',
password: 'XXXXX' )
await connection.connect(
function(err,conn)
if (err)
console.error('Unable to connect: ' + err.message);
else
console.log('Successfully connected');
connection_ID = conn.getId();
console.log(connection_ID);
);
await connection.execute(
sqlText: 'select current_database()',
complete: function(err,stmt, rows)
if (err)
console.error('Failed to execute statement due to the following error: ' + err.message);
else
console.log('Successfully executed statement: ' + stmt.getSqlText());
);
connectToSnowflake();
运行这个脚本我得到:
[local@fedora nodejs]$ node connect_snowflake.js
Successfully connected
90dfc5b0-a509-4d0a-8cfb-8022d02e8603
Successfully executed statement: select current_database()
[local@fedora nodejs]$
尝试相同,看看是否有效。
【讨论】:
以上是关于在 NextJS 中使用 Snowflake 无法连接或执行 getStaticPaths/getStaticProps 中的查询的主要内容,如果未能解决你的问题,请参考以下文章
无法使用 NextJS 9 的 API 路由设置 cookie
无法在 Snowflake 中使用混合数据类型的字段内连接两个简单的 CTE
无法在 NextJS React 组件中获取背景图像渲染 [已解决但无法解释]
无法将数据从 Snowflake 复制到 Azure Blob