Consume web service in Node.js and bulk insert to Oracle Database - sql

I need to consume third party webservice using node js and write it to oracle table . Basically I got the code for getting the data. What is the command to insert bulk data into oracle from node json? I will be getting a huge file and I am parsing it. The code is as below.
Can someone help me with insert command here?
const express = require("express");
const oracledb = require("oracledb");
const cors = require("cors");
const bodyparser = require("body-parser");
const request = require("request");
let app = express();
app.use(cors());
app.use(bodyparser.json());
app.use(bodyparser.urlencoded({extended:false}));
app.get("/demo",(req,res)=>{
oracledb.getConnection({
user:"INFO",
password:"12345",
connectString:"194.234.222.22:1521/TEST"
},(err,connection)=>{
if(err) throw err;
else{
//make request call
try{
request.post({
url: 'https://qmilet.com/api/v1/partners/login',
body: {"user":"_api","password":"_api"},
json: true
}, function(error, response, body){
//console.log(body);
token = body.access_token
console.log(token);
//make api request
//31ae0b786d4958c0a93a459f46a59d67b8a9cff8
try{
//replace thw dynamic token
//Syntax : ${token}
request.post({
url : `https://qmilet.com/api/v1/orders/pull?access_token=${token}&date_from=2020-05-01&date_to=2021-05-30&include_synced=1`
},function(err,response,body){
let obj = JSON.parse(body);
res.send(obj.data);
//how to read parsed data, and insert into oracle db
})
}catch(err){
console.log(err);
}
});
}catch(err){
console.log(err);
}
}
});
});
app.listen(8080,()=>{
console.log("server started");
});
I am using 12.1.0.2 version of Oracle.

Related

Nextjs not able to receive data from expressjs API in correct format

ive got a page which i call an api through getserversideprops to basically get all the data from a table. it used to work fine, but suddenly when i runned it today an error occured which i dont know the cause is. this is the response im getting when i console log the response
what i tried on my own and noticed to "help" was reducing the amount of columns that the API was selecting. For the api i used knex and express. this was the original code that used to work but now does not.
try{
let data = "";
await db.transaction(async (t)=>{
data = await db('sales').transacting(t)
.join('users','users.id','sales.cashier_id')
.join('customer','customer.customer_id','sales.customer_id')
.select('customer.name as customer_name','sales.sales_id','sales.date','sales.payment_type','sales.payment_status','sales.customer_id','sales.transaction_total','sales.total_paid','users.name','sales.shipment_fee','sales.days','sales.sale_type')
})
return data
}catch(e){
console.log(e);
throw e;
}
what i tried was reducing the amount of select columns to i think 5 or 6, but definitely if the amount of columsn i use is under a limit it works. like
try{
let data = "";
await db.transaction(async (t)=>{
data = await db('sales').transacting(t)
.join('users','users.id','sales.cashier_id')
.join('customer','customer.customer_id','sales.customer_id')
.select('customer.name as customer_name','sales.sales_id','sales.transaction_total','sales.date','sales.payment_type')
})
return data
}catch(e){
console.log(e);
throw e;
}
these are the attributes of my table
this is the server.js file of my expressjs
const compression = require('compression');
const express = require("express");
const app = express();
const fs = require('fs');
const http = require('http')
const https = require('https')
const PORT = process.env.PORT || 8000;
var cors = require('cors')
const mainRoutes = require('./api/v1/routes/index')
const cookieParser = require("cookie-parser");
app.use(compression());
app.use(cors( {origin: 'http://localhost:3000',credentials: true}))
// app.use(cors());
app.use(express.json());
// app.use(express.urlencoded({ extended: true }));
app.use(cookieParser());
// Main Routes
app.use("/api/v1", mainRoutes,(req, res, next) => {
res.header('Access-Control-Allow-Origin', '*');
next();
});
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}.`);
});
this is how im calling the api from next
export async function getServerSideProps(context) {
try{
const res = await axios.get("/sales/get-all");
const data = await res.data
return { props: { data } }
}catch(err){
const data = "error"
return { props: { data} }
}
}
where i declared default url of the axios at the app.js file of next
import '../styles/globals.css'
import axios from 'axios';
axios.defaults.baseURL = "http://localhost:8000/api/v1/"
function MyApp({ Component, pageProps }) {
return <Component {...pageProps} />
}
export default MyApp
im not quite sure what the problem is so pardon if the title of the question is not according to the problem i have.
EDIT:
i moved the api to a useeffect call, its now working. but why isnt it working in getserversideprops?

How to fix [Error: Network Error] calling localhost api using react native and express js

I'am currently studying React native and Express JS for my server side. So now I do practicing calling api from express js to react native and I use axios to call http request. My problem is in my cmd it shows that [Error: Network Error]. To be more specific I will show you guys my sample work both server.js and components and sample screenshot Error on my cmd.
[Error: Network Error]
Server JS:
const express = require('express');
var cors = require('cors')
const bodyParser = require('body-parser');
const mysql = require('mysql');
const connection = mysql.createPool({
host : '192.168.61.1',
user : 'root',
password : '',
database : 'sample_db'
});
if(connection) {
console.log(connection);
}
// Starting our app.
const app = express();
var cors = require('cors');
app.use(cors());
// Creating a GET route that returns data from the 'mobile' table.
app.get('/api/readings', function (req, res) {
// Connecting to the database.
connection.getConnection(function (err, connection) {
// Executing the MySQL query (select all data from the 'mobile' table).
connection.query('SELECT * FROM tbl_mobile', function (error, results, fields) {
// If some error occurs, we throw an error.
if (error) throw error;
// Getting the 'response' from the database and sending it to our route. This is were the data is.
res.send(results)
});
});
});
// get the specific meter
app.get('/api/readings/:id', function (req, res) {
// Connecting to the database.
connection.getConnection(function (err, connection) {
// Executing the MySQL query (select all data from the 'mobile' table).
connection.query('SELECT * FROM tbl_mobile WHERE id = ?',req.params.id, function (error, results, fields) {
// If some error occurs, we throw an error.
if (error) throw error;
// Getting the 'response' from the database and sending it to our route. This is were the data is.
res.send(results)
});
});
});
// Starting our server.
app.listen(3000, () => {
console.log('http://192.168.61.1/api/readings');
});
Axios:
axios.get('http://192.168.61.1:3000/api/readings')
.then(function (response) {
// handle success
console.log(response);
})
.catch(function (error) {
// handle error
console.log(error);
})
.then(function () {
// always executed
});
Response:

Azure SQL database is not updating with data

I have created a nodejs azure web app and it is running perfectly fine,
Now I want to get my post data to azure MS SQL DB, I have created a code for that, however sql db is not updating with POST data, can anyone help me with this.
var express = require('express');
var bodyParser = require('body-parser');
var sql = require('mssql');
var port = 8080;
var app = express();`enter code here`
var path = require('path');
//CORS Middleware
app.use(function(req,res,next){
res.header("Access-Control-Allow-Origin","*");
res.header("Access-Control-Allow-Methods","GET,HEAD,POST,PUT,OPTIONS");
res.header("Access-Control-Allow-Headers","Origin,X-Requested-With,contentType,Content-Type,Accept,Authorization");
next();
});
app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, 'views'));
app.use(bodyParser());
app.use(bodyParser.urlencoded({extended: false}));
app.use(express.static(path.join(__dirname, 'public')));
//setup database connection
var dbconfig = {
user:"username",
password:"password",
server : "server_name",
database: "db_name"
};
// ConnectionPool
//connect to the database
var executeQuery = function(res,query){
sql.connect(dbconfig,function(err){
if(err){
console.log("there is a database connection error -> "+err);
res.send(err);
}
else{
// create request object
var request = new sql.Request();
// query to the database
request.query(query,function(err,result){
if(err){
console.log("error while querying database -> "+err);
res.send(err);
}
else{
res.send(result);
sql.close();
}
});
}
});
}
app.get('/', function(req, res){
res.render('index', {
title: 'Hello World',
showTitle:true,
people: ['John', 'Steve', 'Jose']
});
});
app.get('/index', function(req, res){
res.render('index');
});
app.get('/contact', function(req, res){
res.render('contact');
});
app.post('/my_user', function(req, res){
//return res.send(req.body);
console.log(req.body.email + ' and ' + req.body.mobile);
var parameters = [
{ name: 'email', sqltype: sql.NVarChar, value: req.body.email},
{ name: 'mobile', sqltype: sql.NInt, value: req.body.mobile},
{ name: 'msg', sqltype: sql.NVarChar, value: req.body.msg},
];
var query = "insert into forMyCV values(#email, #mobile, #msg);";
executeQuery (res, query, parameters);
return res.redirect('/');
});
app.listen(port);
console.log('Server started on port '+port);
can anyone help me with this explain this to me. why this is happening
I'm afraid there are two issues in your code after I had test it.
According to the tab Connection strings of your Azure SQL Database on Azure portal as the figure below, there is a required parameter encrypt=true in the connection string like for JDBC and others, too.
So the correct config for Azure SQL Database using mssql should be as below.
const config = {
user: '<your username>#<your sql server name>',
password: '<your password>',
server: '<your sql server name>.database.windows.net', // You can use 'localhost\\instance' to connect to named instance
database: '<your database name>',
options: {
encrypt: true // Use this if you're on Windows Azure
}
}
The required options: {encrypt: true} property can not be ignored. Otherwise, it will cause the error.
there is a database connection error -> ConnectionError: Server requires encryption, set 'encrypt' config option to true.
(node:665784) UnhandledPromiseRejectionWarning: ReferenceError: res is not defined
at D:\projects\node\express-demo\mssql-query-demo.js:43:13
at _poolCreate.then.catch.err (D:\projects\node\express-demo\node_modules\mssql\lib\base.js:287:7)
at process._tickCallback (internal/process/next_tick.js:68:7)
(node:665784) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:665784) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
the executeQuery function is defined with two parameters res and query, but it be used with three parameters executeQuery (res, query, parameters); in the callback function(req, res) of app.post('/my_user'). It will not cause any error and just ignore the nonmatched last parameter parameters.
Hope it helps.
Update: Redefine the executeQuery function as below.
var executeQuery = function(res,query, parameters){
sql.connect(dbconfig,function(err){
if(err){
console.log("there is a database connection error -> "+err);
res.send(err);
}
else{
// create request object
var request = new sql.Request();
// query to the database
parameters.map(item => {
request.input(item.name, item.sqltype, item.value);
})
request.query(query,function(err,result){
if(err){
console.log("error while querying database -> "+err);
res.send(err);
}
else{
res.send(result);
sql.close();
}
});
}
});
}

node.js express.js post data

I encountered a problem in getting client POST data as follow:
my client sent:
content-type:application/x-www-form-urlencoded
the content:
{"value":"123456"}
In node.js, it is no problem to parse the content to a json object, my code as follow:
http.createServer(function onRequest(request, response) {
request.setEncoding("utf8");
var content = [];
request.addListener("data", function(data) {
content.push(data); //Collect the incoming data});
//At the end of request call
request.addListener("end", function() {
response.writeHead( 200, {"Content-Type": "text/plain"} );
ms = content[0];
if(ms.toString() != "")
{
var msg = ms.toString(); //Parse the ms into string
console.log(msg); // Prints the message in the console
var reqObj = JSON.parse(msg); // If the incoming message is in JSON format, it can be parsed as JSON.
console.log(reqObj);
response.end(); //Close the response
}
});
Output of the above code:
{"account": "48264"} //before parse to JSON
{ account: '48264' } //after parse to JSON
but when I changed into express.js and use bodyParser, it mixed up.
var express = require('express');
var router = express.Router();
/* GET users listing. */
router.post('/', function(req, res, next) {
req.setEncoding('utf8');
console.log(req.body);
console.log(req.body.value);
});
module.exports = router;
The above outputs in the console as follow:
{ "{\"account\": \"123456\"}": "" }
undefined
I searched the web, but can't find the solution. please help.
Thanks in advance
You need to use express body-parser middleware before defining any routes.
var bodyParser = require('body-parser');
var app = express();
port = parseInt(process.env.PORT, 10) || 8080;
// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: true }));
// parse application/json
app.use(bodyParser.json());
app.listen(port);
app.post("/someRoute", function(req, res) {
console.log(req.body);
res.status(200).json({ status: 'SUCCESS' });
});

res.json() won't work when trying to display data

I am using Nodejs, express, and postgresql to create a rest api. This is my first time doing any of this so I apologize for the noobness. I have been just testing this out and seemed to be about to get retrieve information from a database locally but when I try to send the data with res.json() nothing shows up. Here is the code I have so far.
var express = require('express');
var client = new pg.Client({user: 'xxx', password: 'xxx', database: 'xxx', host: 'xxx'});
var app = express();
app.get('/test1', function(req,res){
var name;
client.connect(function(err){
if(err){
return console.error('could not connect to postgres', err);
}
client.query("select classname from class", function(err, result){
if(err){
return console.error('error running query', err);
}
name = result.rows[0].classname;
console.log(name);
client.end();
});
});
res.send(name);
});
I used the console log and it printed out what I needed but for some strange reason it won't send. Thanks for the help! Also, if you see anything else wrong don't be afraid to say it. Thanks!
It's an asynchronous function so it would go like this:
var express = require('express');
var client = new pg.Client({
user: 'xxx',
password: 'xxx',
database: 'xxx',
host: 'xxx'
});
var app = express();
app.get('/test1', function(req, res) {
var name;
client.connect(function(err) {
if (err) {
return console.error('could not connect to postgres', err);
}
client.query("select classname from class", function(err, result) {
if (err) {
return console.error('error running query', err);
}
name = result.rows[0].classname;
console.log(name);
client.end();
res.send(name);
});
});
});
What is happening in your code is that you are sending before the query is done executing. You can see it on the console because your log statement is correctly executing after the query.
Also you should take care of your errors as well - so instead of returning do another res.send (or res.json) with some error mesg.