在NextJS中使用Snowflake无法在getStaticPaths/getStaticProps中连接或执行查询



我已经对NextJS进行了一段时间的修改,并试图将一个应用程序转换为使用getStaticPropsgetStaticPaths预呈现静态页面,每个页面的[id].js文件都在它们自己的单独文件夹中(比如pages/posts/[id].js、/pages/articles/[id]].js等(。由于这两个功能保证在服务器上运行,我选择将fetch数据库函数从我的公共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如何异步连接存在一些问题,但我不明白为什么";等待";关键字在这种情况下不起任何作用。我可以添加或删除";等待";并且结果是完全相同的。

NextJS依赖于NodeJS,因此按照Snowflake文档中的步骤,我第一次尝试就让它工作了。

这是我的脚本:

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]$ 

试试同样的方法,看看是否有效。

相关内容

  • 没有找到相关文章

最新更新