2016-07-24 161 views
0

從這個問題的後續>Stopping response if document isn't found因爲它被推薦我使用Promise。從貓鼬承諾返回迴應

所以基本的前提是,如果我們無法在數據庫中找到id,節點將返回「無法找到ID」消息。

v1.post("/", function(req, res) { 

    // If the project_id isn't provided, return with an error. 
    if (!("project_id" in req.body)) { 
     return res.send("You need to provide Project ID"); 
    } 

    // Check if the Project ID is in the file. 
    helper.documentExists(ProjectsData, {project_id: req.body.project_id}) 
     .then(function(c) { 
      if (c == 0) { 
       return res.send("The provided Project Id does not exist in our database."); 
      } else { 
       var gameDataObj = req.body; 

       GameData.addGameId(gameDataObj, function (err, doc) { 
        if (err) { 
         if (err.name == "ValidationError") { 
          return res.send("Please send all the required details."); 
         } 
         throw err; 
        }; 

        res.json(doc); 
       }) 
     }; 
    }); 
}); 

而且helper.documentExists

module.exports = { 
    documentExists: function(collection, query) { 
     return collection.count(query).exec(); 
    }, 
}; 

但腳本將繼續在此之後運行並打印「未找到所需的數據」。

Output: 
    required data not found 
    1 

我正在使用原生ES6承諾。

var mongoose = require("mongoose"); 
    mongoose.Promise = global.Promise; 

編輯:包括整個獲取路線。 (以後會解決這些拋ERR)

+1

'console.log(「required data not found」); ''''''''''''會運行任何計數,因爲它超出了承諾然後回調 – Iceman

+0

哦,我認爲Promise會在它低於它的任何東西之前運行。否則阻止? – DragoonHP

+0

是的,你應該。看到我的答案有點洞察力! – Iceman

回答

1
#######POINT 1######### 
ProjectsData.count({project_id: req.body.project_id}) 

    .then(function(c) { 
     #######POINT 3######### 
     if (c == 0) { 
      console.log("1"); 
      return res.send("The provided Project Id does not exist in our database."); 
      console.log("2"); 
     } 
    }); 
#######POINT 2######### 
//some other logic 
console.log("required data not found"); 

繼異步工作流程:1點後,在創建承諾,您的附加處理程序。現在POINT 2將繼續,而(在未來的某個時鐘,承諾已解決,您將達到點3.

由於我對工作流程/目的的理解有限,我只是簡單地將POINT 2代碼放在ifelse{}在POINT 3(正如你在評論中正確猜測的那樣) 編輯:感謝@ jfriend00指出了我以前版本的答案中的一個嚴重錯誤

+1

非常感謝。 :) – DragoonHP

+0

@DragoonHP - 這個答案是錯誤的。有絕對的保證,這個代碼的輸出將是「未找到需要的數據」,然後「1」和「2」不會因爲「返回」而被擊中。所有'.then()'處理程序在未來的時鐘週期內保證異步(按照promoise標準),因此點2總是在1,2和3之間到達。 – jfriend00

+0

@ jfriend00您是對的。只有在下一個滴答聲中才會觸及承諾! – Iceman

1

您的代碼基本上會導致在此:

ProjectsData.count().then(...); 
console.log("required data not found"); 

所以,當然第二console.log()要運行和打印。在.then()處理程序中沒有發生任何事情,直到console.log()已經運行很久。即使如此,它也無法阻止其他代碼的運行。承諾不會讓口譯員「等待」。他們只是爲您提供結構來協調您的異步操作。

如果您想用承諾進行分支,那麼您必須在.then()處理程序內進行分支,而不是在分支之後進行分支。


您沒有足夠展示您正在做什麼以瞭解如何推薦完整解決方案。我們需要查看其餘的請求,以幫助您根據異步結果進行正確的分支。


你可能需要的東西是這樣的:

ProjectsData.count({project_id: req.body.project_id}).then(function(c) { 
    if (c == 0) { 
     return res.send("The provided Project Id does not exist in our database."); 
    } else { 
     // put other logic here 
    } 
}).catch(function(err) { 
    // handle error here 
}); 
+0

添加了一個可能的解決方案,但我們確實需要查看其他相關代碼,以瞭解您實際正在嘗試執行的操作。 – jfriend00

+0

無法在評論中發佈整個代碼,所以我會盡量讓它失去相位>檢查項目是否存在>如果是,請檢查提供的數據是否有效>如果是,請將文檔添加到mongodb集合中。 – DragoonHP

+1

@DragoonHP - 我問你在你的問題中使用「編輯」在那裏包括相關的代碼。當您展示整個問題並展示真實代碼時,我們可以幫助更完全。 – jfriend00