How to properly use nodejs soap - javascript

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);
});
});

Related

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.

Connection.query to execute

Pretty sure this is a quite noobish node.js/callback question but I can't seem to find the proper code to make it run.
This is how I invoke my node-mysql code:
var utils = require('../../config/database/utils');
exports.getResults = function(callback) {
var query = "SELECT * FROM my_table";
utils.exec(query, null, function(err, results){
if(err){
console.log(err);
callback(true);
return;
}
console.log(results);
callback(false, results);
});
};
Next is the utils file where I can't get the code work.
var pool = require('./connection');
module.exports = {
getDBConnection: function() {
pool.getConnection(function(err, connection){
if(err){
console.log(err);
return;
}
return connection;
});
},
endDBConnection: function(connection) {
connection.end(function (err) {
if(err) {
console.log(err);
callback(true);
return;
}
});
},
exec: function(query, data, callback) {
console.log(query);
this.getDBConnection(function(err, connection){
if(err){
console.log('error');
}
console.log(connection);
connection.query(query, data, function(err, results) {
if(err) {
callback(err);
}
callback(false, results);
});
this.endDBConnection(connection);
});
}
}
Code is getting OK the the exec part since the console.log(query) logs the query. But after that, the code's not running, console.log(connection); doesn't show a thing, and of course the connection.query is also not running.
I'm not sure why this is happening.
Returning a value inside a callback is meaningless. You need to pass in a callback that gets called with the value you want to return:
getDBConnection: function(callback) {
pool.getConnection(function(err, connection){
if(err){
console.log(err);
return callback(err);
}
callback(null, connection);
});
},
You should also use connection.release() instead of connection.end() since you are using a pool:
endDBConnection: function(connection) {
connection.release();
},
In exec(), you have the wrong this. It should instead be something like:
exec: function(query, data, callback) {
console.log(query);
var self = this;
this.getDBConnection(function(err, connection){
if(err){
console.log('error');
return callback(err);
}
console.log(connection);
connection.query(query, data, function(err, results) {
self.endDBConnection(connection);
if(err) {
return callback(err);
}
callback(null, results);
});
});
}

Is there a better way of dealing with concurrency in Mongo in Node?

I'm working on an API where we send the Last-Modified date header for individual resources returned from our various GETs.
When a client is making a PUT/PATCH on that resource, they can send an If-Unmodified-Since header to ensure that they are only updating the most current version of the resource.
Supporting this is kind of a pain because I want my app to respond to the following use-cases:
the resource you're trying to update doesn't exists (404)
the resource you're trying to update fails a precondition (412)
there was an error processing the request (500)
Is there a better way to do this with Mongo which doesn't involve making 3 separate Mongo db calls in order to capture the possible use cases and return the appropriate response?
I'd like to clean this up:
// the callback the handler is expecting from the model method below
callback(err, preconditionFailed, wasUpdatedBool)
// model method
var orders = this.db.client({ collection: 'orders' });
var query = { _id: this.db.toBSON(this.request.params.id) };
// does the order exist?
orders.findOne(query, function(err, doc) {
if(err) {
return callback(err);
}
if(!doc) {
return callback(null, null, false);
}
// are you updating the most recent version of the doc?
var ifUnModifiedSince = new Date(self.request.headers['if-unmodified-since']).getTime();
if(ifUnModifiedSince) {
query.lastModified = { $lte: ifUnModifiedSince };
orders.findOne(query, function(err, doc) {
if(err) {
return callback(err);
}
if(!doc) {
return callback(null, true);
}
//attempt to update
orders.update(query, payload, function(err, result) {
if(err) {
return callback(err);
}
if(!result) {
return callback(null, null, false);
}
return callback(null, null, result);
});
});
}
//attempt to update
orders.update(query, payload, function(err, result) {
if(err) {
return callback(err);
}
if(!result) {
return callback(null, null, false);
}
callback(null, null, result);
});
});
You have too many queries, as you probably know. The general flow of this should be:
Look for the document by ID - if not found, give a 404
Check the presence of the if-unmodified-since header and compare it with the document's modified date. Issue 412 if applicable
Update the document if allowed
So, your code should look something like this:
var orders = this.db.client({ collection: 'orders' });
var query = { _id: this.db.toBSON(this.request.params.id) };
orders.findOne(query, function(err, order) {
if(err) {
return callback(err); // Should throw a 500
}
if(!order) {
return callback(404);
}
if(self.request.headers['if-unmodified-since']) {
var ifUnModifiedSince = new Date(self.request.headers['if-unmodified-since']).getTime();
if(order.lastModified.getTime() > ifUnModifiedSince) {
return callback(412); // Should throw a 412
}
}
// Now do the update
orders.update(query, payload, function(err, result) {
if(err) {
return callback(err);
}
if(!result) {
return callback(null, null, false);
}
return callback(null, null, result);
});
});

How to wait for a response from a mongo findOne query in a node/express app before using the response in following control flow

I am new to node, and also JavaScript callbacks.
I am trying to check if an account exists in mongo and then 'save it' if it doesn't and return an error if it does.
I am currently trying to figure this out outside of my express app. This is what i have..
var MongoClient = require('mongodb').MongoClient;
MongoClient.connect('mongodb://localhost:27017/main', function (err, db) {
if(err) throw err;
var query = { name : "www.website.com"}
findOne(db, query, function (doc) {
if(doc) {
console.log('account exists');
} else {
console.log('good to go');
}
console.dir(doc);
});
});
var findOne = function (db, query, callback) {
db.collection('accounts').findOne(query, function (err, doc) {
if(err) throw err;
db.close();
callback();
});
}
with the console.dir(doc); above returning as undefined. How do I wait for the findOne to return before using the callback to console.log or save the account?
The reason you are getting undefined is because when you call your callback your are not passing it the doc. That line should look like callback(doc).
Here is an updated version of your code with a few suggestions:
MongoClient.connect('mongodb://localhost:27017/main', function (err, db) {
if(err) throw err;
var query = { name : "www.website.com"}
findOne(db, query, function (err, doc) {
if(err) {
// something went wrong
console.log(err);
return;
}
if(doc) {
console.log('account exists');
console.dir(doc);
} else {
console.log('good to go');
}
});
});
var findOne = function (db, query, callback) {
db.collection('accounts').findOne(query, function (err, doc) {
db.close();
if(err) {
// don't use throw when in async code
// the convention is to call your callback with the error
// as the first argument (notice that I added an argument
// to the definition of your callback above)
callback(err);
}
else {
// call your callback with no error and the data
callback(null, doc);
}
});
}

express.js and soap calls

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);
});
});

Categories

Resources