我编写了以下SOAP服务器代码。它查询数据库(mongoDB)返回结果。但是使用回调方法它返回null。任何建议如何返回从DB获取的值。
var soap = require('soap');
var http = require('http');
var wait=require('wait.for-es6');
//This has details of Mongoose schema to connect to mongoDB
var models = require('./models');
var getLocation = function(callback){
navigator.geolocation.getCurrentPosition(function(pos){
succesfull(pos);
typeof callback === 'function' && callback(geoloc);
}, function(){
alert("fail");
});
};
var service = {
ws: {
calc: {
sumar : function(args) {
var n = args.a + args.b;
return { res : n };
},
multiplicar : function(args) {
//console.log(args)
var n = args.a * args.b;
return { res : n }
},
GetAccessList : function(args){
//console.log(args);
var AccessName = args.AccessPName;
var AccessType = args.AccessPType;
console.log("Access Point Name = " + AccessName);
console.log("Access Point Type = " + AccessType);
var data1 = null;
//Get the details of the Access Point
models[AccessType].findByName(AccessName,{},function(err,data){
if(err) return callback(err);
data1 = data;
//Get details of the License plate for the selected access point
models["LprSensor"].readByLocationId(data._id,{},function(err,data){
if(err) return callback(err);
//console.log(data);
});
}));
console.log(data1);
return { res : data1}
}
}
}
}
var xml = require('fs').readFileSync('wscalc1.wsdl', 'utf8'),
server = http.createServer(function(request,response) {
response.end("404: Not Found: "+request.url)
});
server.listen(8000);
soap.listen(server, '/wscalc1', service, xml);
如何等待回调并将值返回给客户端。
答案 0 :(得分:1)
我认为您提出的问题是如何使此代码无阻塞,而不是使用return
代替callback
。
请参阅node-soap, A SOAP client and server for node.js。
// This is how to define an asynchronous function.
MyAsyncFunction: function(args, callback) {
// do some work
callback({
name: args.name
});
}
在没有回调的情况下进行比较:
MyFunction: function(args) {
return {
name: args.name
};
}
使用回调而不是返回。