Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/471.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/42.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
已跳过Node.js JavaScript片段中的代码_Javascript_Node.js - Fatal编程技术网

已跳过Node.js JavaScript片段中的代码

已跳过Node.js JavaScript片段中的代码,javascript,node.js,Javascript,Node.js,我有一些代码如下所示: function write_to_orchestrate(data_to_write) { console.log('more testing'); db.put('musician', '1', '{"test":"test1"}') .then(function (result) { res.send(result); }) .fail(function (err) {

我有一些代码如下所示:

function write_to_orchestrate(data_to_write) {

    console.log('more testing');

    db.put('musician', '1', '{"test":"test1"}')
        .then(function (result) {
            res.send(result);
        })
        .fail(function (err) {
            res.send(err);
        });

    console.log('something');

}
看起来,即使我在db.put函数调用的.then和.fail中的res.send()函数调用上有断点,但它们中的任何一个都不会停止。然而,console.log(“更多测试”);和console.log('something');无误地到达

我正在使用Webstorm,但即使我只是使用终端和文本编辑器定期执行此代码,db.put(…)的代码似乎也被跳过了。关于额外的故障排除/调试有什么想法吗

我还注意到,此调用发出put to的数据源也是活动的并接受命令。为了确保这一点,我发出了几个curl命令,以确保数据的输入和输出都很好

下面是一段视频,解释正在发生的事情:

更新#1:我还尝试了根据建议将{“test”:“test1”}改为{“test”:“test1”},这似乎是个好主意,但仍然没有成功

更新#2:Per@haftor我尝试了另一个建议,并实现了类似这样的代码,以消除可能出现的分号问题

function write_to_orchestrate(data_to_write) {

    console.log('more testing');

    db.put('musician', '1', {"test":"test1"}).then(function (result){res.send(result);}).fail(function (err) {res.send(err);});

    console.log('something');

}
它仍然没有,但在WebStorm中运行代码并调试时,我确实发现代码到达了db.put,但随后我尝试进入代码,它直接进入了.fail,然后跳出并最终到达console.log('something');线路


另外,还有另一个视频,可以尝试更多地实现这一点:

尝试将
{“test”:“test1”}
替换为
{“test”:“test1”}
尝试将
{“test”:“test1”}
{test1”}只是想一想,您是否尝试过重载db.put函数


var\u put=db.put;db.put=function(a,b,c){console.log('yesp');return{u put(a,b,c);}

只是一个想法,您是否尝试过重载db.put函数


var\u put=db.put;db.put=function(a,b,c){console.log('yesp');return _put(a,b,c)}

在观看第二个视频时,您似乎希望函数在逐步完成其分配时执行。(例如)。您实际做的是分配db.put函数(a、b、c),而不是执行它。所以,正如你在视频中提到的,它跳过身体是有道理的

然而,它最终应该执行它。它是否跳过了您在其中设置的断点

由于我对Webstorm一无所知,我认为让它进入then and fail函数的最可靠方法是使用显式的调试器语句,如下所示:

function write_to_orchestrate(data_to_write) {

console.log('more testing');

db.put('musician', '1', '{"test":"test1"}')
    .then(function (result) {
        debugger;
        res.send(result);
    })
    .fail(function (err) {
        debugger;
        res.send(err);
    });

console.log('something');

}
然后通过本机节点运行它:


在调试器中,键入C以继续,然后当它点击调试器语句时应该停止。

在观看第二个视频时,您似乎希望在逐步完成函数的分配时执行这些函数。(例如)。您实际做的是分配db.put函数(a、b、c),而不是执行它。所以,正如你在视频中提到的,它跳过身体是有道理的

然而,它最终应该执行它。它是否跳过了您在其中设置的断点

由于我对Webstorm一无所知,我认为让它进入then and fail函数的最可靠方法是使用显式的调试器语句,如下所示:

function write_to_orchestrate(data_to_write) {

console.log('more testing');

db.put('musician', '1', '{"test":"test1"}')
    .then(function (result) {
        debugger;
        res.send(result);
    })
    .fail(function (err) {
        debugger;
        res.send(err);
    });

console.log('something');

}
然后通过本机节点运行它:

在调试器中,键入C以继续,然后在遇到调试器语句时应停止

  • 无法显式显示正在使用的数据库模块。根据上下文线索,这是正确的吗
  • 天哪,youtube!把你的代码放在你的问题里。包括初始化数据库的位置,这是最重要的部分,它允许您通过更改此处发布的文本来隐藏您的令牌。。。你要么想取下这些视频,要么改变你的代币,因为你在第一个视频中毫无希望地破坏了你的安全。是的,很模糊,但不是那么模糊
  • 好的,第一个故障排除步骤是创建一个简化脚本。我猜你不正确地使用了这个模块。试试这个:

    var db = require('orchestrate')('SUPER SECRET TOKEN THAT SHOULD NOT BE IN A YOUTUBE VIDEO');
    
    console.log('before query');
    db.put('musician', '1', {test: 'test1'})
        .then(function (result) {
            console.log('THEN!!!');
            console.log(result);
        })
        .fail(function (err) {
            console.log('FAIL!!!');
            console.log(err);
        });
    console.log('after query');
    
    。。。这里有几件重要的事情我想让你注意:

    • 隐藏您的令牌(认真地说,隐藏您的令牌)
    • 我是根据我链接的模块的文档工作的,但是如果您使用了不同的东西或者以任何方式修改了它,所有这些建议都是可疑的
    • 根据@nhaa123s注释和文档,该值是一个对象文本,而不是JSON字符串,因此我进行了调整
    • 根据链接的文档,
      music
      是集合名,
      1
      是您试图设置的键(这似乎是一个奇怪的键名,但无论如何,{test:'test1'}是您用于值的数据结构。我从未使用过orchestrate,如果需要,请确保您已正确初始化
      Musitor
      ,并确认它将支持严格的数字键,如
      1
      (或者,更好的是,使用更传统的键类型,如
      testkey
    • 我使用了console.log,因此您可以确认回调的执行更加简单
    。。。根据问题的实际位置,您可能会看到几种不同的结果。如果正确使用数据库模块,则应执行
    .then()
    回调,并知道问题在于如何将其合并到代码中。如果它失败了,并且您在控制台上收到了失败消息,您将看到实际的错误,这可能会引导您走向正确的方向。如果从未调用
    .then()
    .fail()
    回调,则orchestrate模块可能尚未准备就绪,或者依赖项链中存在某些不兼容

    如果可以成功执行该代码段,那么下一步就是复制
    db.put().then()。
    
    function write_to_orchestrate(data_to_write) {
        if (res === undefined) console.log("This is not the response you're looking for");
        else console.log("Just kidding, you're (probably) good");
    
        // etc.
    }
    
    function write_to_orchestrate(data_to_write, res) {
        if (res === undefined) console.log("The problem clearly exists even further up the chain");
        else console.log("Ah, just as we expected");
    
        // etc.
    }
    
    // initial stack
    console.log('more testing');
    db.put(...);
    console.log('something');
    
    // ... time passes, the call stack is cleared
    // ... the database returns a status message
    // new stack:
    res.send(result);
    
    db.put('musician', '1', '{"test":"test1"}')
        .then(function (result) {
            res.send(result);
        })
        .fail(function (err) {
            res.send(err);
        })
        .end(); /// <<< Add this at the end of your chain.