【发布时间】:2016-11-29 13:38:32
【问题描述】:
我正在使用vpulim:node-soap 来运行一个肥皂服务器。
我的流星服务器启动包含这个以及其他各种代码:
authRequestOperation: function(args,cb,headers,req) {
console.log(args);
var authResponceObject = {};
var futureAuthResponse = new Future();
Fiber(function(){
if(collectorUsers.findOne({username: args.username})){
console.log("Found User");
authResponceObject = {
username: args.username,
nonce: Random.id()
};
console.log("authResponceObject is: " + JSON.stringify(authResponceObject,null,4));
console.log("futureAuthResponse returning...");
futureAuthResponse.return(authResponceObject);
}
// console.log("futureAuthResponse waiting...");
// return futureAuthResponse.wait();
}).run();
console.log("authResponceObject after fiber is: " + JSON.stringify(authResponceObject,null,4));
return authResponceObject;
},
我想做的是:
- 我从客户端收到一个用户对象。
- 我检查用户是否存在于 mongodb 中
- 如果用户在场,准备响应对象
- 使用响应对象响应客户端。
我有 1. 工作。但是,由于它是异步调用,所以 2,3,4 的顺序是混乱的。
现在发生的事情是:
- 接收客户端对象
- 返回响应对象(为空)
- 检查 mongo
- 准备响应对象。
我没有在上面使用 Meteor.methods。
我如何以正确的方式完成这项工作?我尝试过在wrapAsync 和fiber/future 之间玩转,但遇到了死胡同。
【问题讨论】:
-
为什么在上面的代码中使用
Fiber?.findOne已经同步。如果不使用Fiber,您会看到错误吗? -
因为我在 Meteor.methods 之外运行代码,所以我需要将它包装在 Fiber 中。否则我会收到一条错误消息,说 Meteor 需要使用光纤运行。
标签: javascript node.js meteor soap node-soap