2016-05-13 91 views
0

我有一個解析服務器安裝expressjs像hereexpressjs不會拋出解析查詢錯誤(解析服務器)

但有時它不會在解析函數中顯示錯誤。例如:顯示

// 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

我只想讓我的解析函數中顯示錯誤。

任何想法?

謝謝你的幫助!

回答

0

您的問題實際上是由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:趕上()是不完全一樣則(NULL,...)」。

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

+0

好的謝謝你! – Poltib