Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/461.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 链接异步回调节点_Javascript_Node.js_Asynchronous_Orientdb_Gremlin - Fatal编程技术网

Javascript 链接异步回调节点

Javascript 链接异步回调节点,javascript,node.js,asynchronous,orientdb,gremlin,Javascript,Node.js,Asynchronous,Orientdb,Gremlin,我正在使用OrientDB进行项目,所以我使用gremlin graph语言来实现一些功能,我对异步回调链接感到困惑,我想要这样的指令 var length = g.getEdges().count(); 例如,getEdge()是一个异步函数 var grex = require('grex'); var async = require('async'); var gremlin = grex.gremlin; var g = grex.g; var client = grex.create

我正在使用OrientDB进行项目,所以我使用gremlin graph语言来实现一些功能,我对异步回调链接感到困惑,我想要这样的指令

var length = g.getEdges().count();
例如,getEdge()是一个异步函数

var grex = require('grex');
var async = require('async');
var gremlin = grex.gremlin;
var g = grex.g;
var client = grex.createClient(
{
    host: 'localhost',
    port: 8182,
    graph: 'orientdbsample'
}
);

function OrientGraph(obj) {
    this.result = obj.result;
    this.results = obj.results;

    this.getEdges = function(callback) {
        async.auto({
            getV: function(cb) {
                client.fetch(g.E(), function(err, response) {
                    if (err) return cb(err);
                    return cb(null, response);
                });
            }
        }, function(err, res) {
            callback(err, new OrientGraph({
                results: res.getV
            }));
        });
    }

    this.count = function(){
        return this.results.length;
    }
module.exports = OrientGraph; 
}
在我使用的Server.js文件中

var GremApi = require('./app/Api');
var og = new GremApi({
  results : []
});
og.getEdges(function(err,data){
  console.log(data.count());
}).count();
错误是:

/home/xar/dev/stage/gremlin/server.js:28
}).count();
  ^
TypeError: Cannot read property 'count' of undefined
    at Object.<anonymous> (/home/xar/dev/stage/gremlin/server.js:28:3)
    at Module._compile (module.js:460:26)
    at Object.Module._extensions..js (module.js:478:10)
    at Module.load (module.js:355:32)
    at Function.Module._load (module.js:310:12)
    at Function.Module.runMain (module.js:501:10)
    at startup (node.js:129:16)
    at node.js:814:3
/home/xar/dev/stage/gremlin/server.js:28
}).count();
^
TypeError:无法读取未定义的属性“count”
反对。(/home/xar/dev/stage/gremlin/server.js:28:3)
在模块处编译(Module.js:460:26)
在Object.Module.\u extensions..js(Module.js:478:10)
在Module.load(Module.js:355:32)
在Function.Module.\u加载(Module.js:310:12)
位于Function.Module.runMain(Module.js:501:10)
启动时(node.js:129:16)
在node.js:814:3

谢谢

您无法从异步函数返回,因此需要将
length
作为参数传递给传递给
count
的回调函数。这两篇文章介绍了如何执行此操作。1.2.基本上,您需要创建一个helper类来存储您的
count()
方法,并在前面的函数“完成”(调用了它们的回调)后执行它。看起来您的问题与使用
async.auto
有关,我不太熟悉。我建议您查看一下,您不能从异步函数返回,因此您需要将
length
作为参数传递给传递给
count
的回调。这两篇文章解释了如何执行此操作。1.2.基本上,您需要创建一个helper类来存储您的
count()
方法,并在前面的函数“完成”(调用了它们的回调)后执行它。看起来您的问题与使用
async.auto
有关,我不太熟悉。我建议您查看一下,您不能从异步函数返回,因此您需要将
length
作为参数传递给传递给
count
的回调。这两篇文章解释了如何执行此操作。1.2.基本上,您需要创建一个helper类来存储您的
count()
方法,并在前面的函数“完成”(调用了它们的回调)后执行它。看起来您的问题与使用
async.auto
有关,我不太熟悉。我建议你看一下