【问题标题】:expressjs not throwing error on Parse queries (parse-server)expressjs 不会在解析查询(解析服务器)上抛出错误
【发布时间】:2016-09-09 08:58:50
【问题描述】:

我在 expressjs 上有一个解析服务器设置,例如 here

但有时它不会在 Parse 函数中显示错误。示例:

// Parse Server is setup
// Parse Server plays nicely with the rest of your web routes
app.get('/', function(req, res) {
  var pageQuery = new Parse.Query('Page');
  pageQuery.get('id').then(function(page) {
    someObject.undefinedProp = false;
    res.send(page);
  }, function(error) {
    res.send(error);
  });
});

没有显示错误,但使用以下代码:

// Parse Server is setup
// Parse Server plays nicely with the rest of your web routes
app.get('/', function(req, res) {
  someObject.undefinedProp = false;
  res.send('ok');
});

我显示了这个错误:

ReferenceError: someObject is not defined

(对于这个示例,我的配置与Parse Server Example 完全相同)

我只想在我的 Parse 函数中显示错误。

有什么想法吗?

感谢您的帮助!

【问题讨论】:

    标签: express parse-server


    【解决方案1】:

    你的问题其实是 Promises 引起的问题。

    当您调用pageQuery.get('id') 时,get 方法会返回一个 Promise 实例。 Promise 的then 方法是您设置回调的方式,该回调将在get 操作成功完成时触发。

    为了获得对在您尝试引用 someObject.undefinedProp 时应该发生的错误的引用,您还需要通过调用其 catch 方法在该 Promise 对象上设置错误处理程序。

    app.get('/', function(req, res) {
      var pageQuery = new Parse.Query('Page');
    
      pageQuery.get('id').then(function(page) {
        someObject.undefinedProp = false; 
        // the error thrown here will be caught by the Promise object
        // and will only be available to the catch callback below
        res.send(page);
    
      }, function(error) {
        // this second callback passed to the then method will only
        // catch errors thrown by the pageQuery.get method, not errors
        // generated by the preceding callback
        res.send(error);
    
      }).catch(function (err) {
        // the err in this scope will be your ReferenceError
        doSomething(err);
      });
    });
    

    在这里,查看以下文章并向下滚动到标题“高级错误 #2:catch() 与 then(null, ...) 不完全相同”部分。

    https://pouchdb.com/2015/05/18/we-have-a-problem-with-promises.html

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-07-25
      • 2019-11-01
      • 2012-10-27
      • 2020-01-31
      • 2016-09-25
      • 1970-01-01
      • 1970-01-01
      • 2021-10-14
      相关资源
      最近更新 更多