Reputation: 11
I am trying to design an API using Snowflake and Nodejs. For that I am using the following things :
Express ejs snowflake-sdk (nodejs module)
I want to fetch data from snowflake and want to display it on my ejs webpage. Please help if anyone has fetched data and populated it on a webpage using nodejs and snowflake.
this is my server.js file
const express = require("express");
const app= express();
const sql = require("./snowflake");
app.use(express.static("public"));
app.use(express.urlencoded({ extended: true}));
app.set("view engine","ejs");
app.get("/", function(request,response){
response.render("index");
});
app.get("/request/:core", async function(request,response){
let core=await sql.getCore(request.params.core_name);
response.render("request",{request: core});
});
const http = require('http');
const port=3000;
const server=http.createServer(function(req,res){
})
const listener = app.listen(port,function(error){
if(error){
console.log("Something went wrong due :", error);
}
else{
console.log('Server is listening port '+port);
}
})
This is my database.js file. I am able to connect to snowflake and run queries but can't understand, how to fetch the query result on the ejs webpage.
const { initParams } = require('request');
const sql = require('snowflake-sdk');
const connection = sql.createConnection({
account: 'account_name',
authenticator: 'SNOWFLAKE',
username: 'username',
password: 'password',
database: 'database',
schema: 'schema'
});
module.exports.getCore = async() =>{
connection.execute({
sqlText: 'Select column from Table_name',
complete: async function(err,stmt,rows){
let pool= await sql.connect();
return rows;
}
})
}
Upvotes: 1
Views: 2998
Reputation: 1
Your async function getCore() returns before your query statement ever completes from Snowflake.
Rewrite your function as follows:
module.exports.getCore = async() =>{
return new Promise((resolve, reject) => {
connection.execute({
sqlText: 'Select column from Table_name',
complete: async function(err,stmt,rows){
let pool= await sql.connect();
resolve(rows);
}
}
})
Also keep in mind there is a 16 MB limit on data size per request (https://docs.snowflake.com/en/developer-guide/node-js/nodejs-driver-execute). If data is expected to be larger, then you will need to execute your query in batches or stream data back.
Upvotes: 0
Reputation: 1021
There is a sample application that you can try to compare, is written on node.js. It is a Citi Bike dashboard that lets users view bike usage over time and in differing weather conditions. The source code is available on GitHub.
More details: https://quickstarts.snowflake.com/guide/data_app/#4
Upvotes: 1