express.js and soap calls - javascript

I am using express.js, node.js and node-soap.js and I am getting an error I can not fix.
I have the following code which fails with an exception and I can not see anything in the exception.
var soap = require('soap');
var url = 'http://www.webservicex.net/stockquote.asmx?WSDL';
var args = {
symbol : 'AMZN'
};
soap.createClient(url, function(err, client) {
client.GetQuote(args, function(err, result) {
if (err) {
console.log(err);
return;
}
console.log(result);
});
});
and in the console all I see is:
{ GetQuoteResult: [ 'exception' ] }
Thoughts?
Reza

this is the code that finally got working...
var args1 = {"tns:symbol": "AMZN"};
var url1 = "http://www.webservicex.net/stockquote.asmx?WSDL";
soap.createClient(url1, function(err, client) {
console.log(client.describe().StockQuote.StockQuoteSoap.GetQuote);
client.StockQuote.StockQuoteSoap.GetQuote(args1, function(err, result) {
if (err) {
console.error(err);
return;
}
console.log(result);
});
});

Related

Connection to Mongodb through Node.js

Hii Everyone
In my code, There is an API that works fine.
And I'm trying to connect to MongoDB from Node for the sake of inserting the data from the API.
As you can see, I get this error -
"message": "Uncaught SyntaxError: Unexpected identifier",
it looks like there is a problem with MongoClient.
I looked at the answers on this site about those topics and no one solves my problem.
Thanks for any help!
let http = require('http');
let weatherKey = process.env.weatherKey;
// console.log(weatherKey);
function getData(cb) {
http.get(`http://api.openweathermap.org/data/2.5/weather?q=israel&appid=${weatherKey}`, res => {
let data = ""
res.on('data', string => {
data += string
})
res.on('end', () => {
let obj = JSON.parse(data)
cb(obj);
})
}).on("error", error => {
cb(error);
})
}
var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://127.0.0.1:27017/";
function connectToMongo(data)
MongoClient.connect(url, function (err, db) {
if (err) throw err;
var dbo = db.db("weather-db");
dbo.collection("node").insertOne(data, err => {
if (err) throw err;
console.log("documents inserted");
db.close();
});
});
getData(connectToMongo);
Your function is missing { after (data)
You were missing {} in your code.
let http = require('http');
let weatherKey = process.env.weatherKey;
function getData(cb) {
http.get(`http://api.openweathermap.org/data/2.5/weather?q=israel&appid=${weatherKey}`, res => {
let data = ""
res.on('data', string => {
data += string
})
res.on('end', () => {
let obj = JSON.parse(data)
cb(obj);
})
}).on("error", error => {
cb(error);
})
}
var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://127.0.0.1:27017/";
function connectToMongo(data){
MongoClient.connect(url, function (err, db) {
if (err) throw err;
var dbo = db.db("weather-db");
dbo.collection("node").insertOne(data, err => {
if (err) throw err;
console.log("documents inserted");
db.close();
});
});
}
getData(connectToMongo);

How to properly use nodejs soap

My code looks like this:
soap.createClient(url, function(err, client) {
if(err) {
res.status(500);
return res.send(err);
}
client.GetMemberPIN({pUserName: 'r'}, function(error, result) {
if(error) {
res.status(500);
return res.send(error)
}
return res.send(result);
});
});
I tried running it and it returns this error?
{
"code": "ECONNRESET"
}
I'd suggest testing a few things:
Make sure the url points to a valid WSDL document, e.g. https://www.crcind.com/csp/samples/SOAP.Demo.CLS?WSDL=1
Log which part of the process fails, e.g. the client creation, or the function call.
Here's a working example testing against a public server, this might help you to understand what could be going wrong with your code:
const soap = require('soap');
const url = 'https://www.crcind.com/csp/samples/SOAP.Demo.CLS?WSDL=1';
const args = { id: 1 };
soap.createClient(url, function(err, client) {
if (err) {
console.error("An error occurred creating client:", err);
return;
}
client.FindPerson(args, function(err, response) {
if (err) {
console.error("An error occurred calling client.FindPerson:", err);
return;
}
console.log("client.FindPerson: response:", response);
});
});

NodeJS Html-pdf: fs.readfilesync how to async/await

I have a problem with my html-pdf document creation. The problem is that often the code runs to fast to complete the process of pdf-docutment creation. The Processes consists out of building an HTML-String by replacing placeholders in an Html file. Below you see the code what happens afterwards.
Object.keys(setter).forEach(function(element, key, _array) {
var regex = new RegExp(element, "g");
data = data.replace(regex, setter[element])
})
var result = data;
fs.writeFile(mergeFileRes, result, 'utf8', function (err) {
if(err) {
console.log(err);
return;
} else {
let html2 = fs.readFileSync(mergeFileRes, 'utf8');
let options = {
format: 'a4' ,
"directory" : "/tmp",
};
if(html2){
pdf.create(html2, options).toStream(function(err, stream2){
if(err) console.log(err);
stream2.pipe(res);
stream2.on('end', function () {
try{
fs.unlink(mergeFileRes)
console.log(3090, "deleted file");
}
catch (err){
console.log(3090, "Did not delete file");
}
});
});
} else {
}
}
});
My problem is that in many cases the html2 variable is not yet created before the pdf.create process starts. This is probably because the readFileSync takes too long to finish.
I was wondering, how can I fix this. How can I make the pdf.create wait for the readFileSync to finish and the html2 variable to be filled.
You can use fs.readFile to read the file asynchronously and html2 will be available within the callback function.
Object.keys(setter).forEach(function(element, key, _array) {
var regex = new RegExp(element, "g");
data = data.replace(regex, setter[element])
})
var result = data;
fs.writeFile(mergeFileRes, result, 'utf8', function (err) {
if(err) {
console.log(err);
return;
} else {
fs.readFile(mergeFileRes, 'utf8', function(err, html2){
if (err) throw err;
let options = {
format: 'a4' ,
"directory" : "/tmp",
};
pdf.create(html2, options).toStream(function(err, stream2){
if(err) console.log(err);
stream2.pipe(res);
stream2.on('end', function () {
try{
fs.unlink(mergeFileRes)
console.log(3090, "deleted file");
}
catch (err){
console.log(3090, "Did not delete file");
}
});
});
});
}
});

Mongodb find() return undefined

When ever I try to just use a simple find() for my mongodb it returns undefined.
var MongoClient = require('mongodb').MongoClient;
var url = 'mongodb://localhost:27017/local';
MongoClient.connect(url, function (err, db) {
db.collection('pokemon').find({ $search: { $text: 'Pikachu' } }).toArray(function(err, data){ console.log(data) })
});
EDIT:
Turns out I never created an index by putting
db.collection('pokemon').createIndex({Name: 'text'})
before all the code.
First of all, every time where you have:
function(err, data){ console.log(data) }
you should check errors:
function (err, data) {
if (err) {
console.log('Error:', err);
} else {
console.log('Data:', data);
}
}
Then you will probably see what's going on.
This is also true for the database connection itself - instead of:
MongoClient.connect(url, function (err, db) {
// use db here
});
you should handle errors:
MongoClient.connect(url, function (err, db) {
if (err) {
// handle errors
} else {
// use db here
}
});
If you don't handle errors then don't be surprised that you don't know why you don't get values.

How to pass data from Server to Client in Meteor

I am Learning Meteor and Javascript. I am using an npm package to get meta data of an url on the server side. This works fine. But I get undefined when passing that result back to client. Would appreciate some help.
Here is my code
if (Meteor.isClient) {
Meteor.call('getMetaData', "http://www.bbc.co.uk/news", function (err, data) {
if (err) {
console.log("error", err);
};
console.log("Meta data: " + data); //shows undefined
});
}
if (Meteor.isServer) {
Meteor.startup(function () {
var preview = Meteor.npmRequire('page-previewer');
Meteor.methods({
getMetaData: function (url) {
preview(url, function (err, data) {
if (!err) {
console.log(data); //Works fine
return data;
}
});
}
})
});
}
You need to convert the preview function to an synchronous function,using Future like this, this will make this function wait normal err,data callbacks into a synchronous function.
var Future = Npm.require('fibers/future'),
preview = Meteor.npmRequire('page-previewer');
Meteor.methods({
getMetaData: function(url) {
var f = new Future();
preview(url, function(err, data) {
if (!err) {
return f.return(data);
}
});
return f.wait();
}
});
Now this snippet should work
if (Meteor.isClient) {
Meteor.call('getMetaData', "http://www.bbc.co.uk/news", function (err, data) {
if (err) {
console.log("error", err);
}else{
console.log("Meta data: " + data); //shows undefined
}
});
};
try using else block to get the meta data. here's a solution of a similar problem .
https://forums.meteor.com/t/client-getting-undefined-for-server-method/6129/4?u=faysal
so basically you need to add only one extra line
else{ console.log('metadata '+ data);}

Categories

Resources