2015-10-05 68 views
2

我是Express和Node的新手,有一個我不明白的問題。我已經定義了兩個不同的Express路由來爲兩個不同的Mongoose數據模型發出POST請求。一個正常工作,並正常返回,另一個在Heroku日誌中返回204的狀態,然後超時,就像保存甚至不執行...如何在Express POST路線中使用Mongoose保存方法

這裏是「犛牛」模型和相關的POST路線保存對象,並返回預期結果回到我的角端:

模型定義:

var mongoose = require('mongoose'); 
var Schema = mongoose.Schema; 

var YakSchema = new Schema({ 
catagory: String, 
loc: { 
    type: [Number], // [<longitude>, <latitude>] 
    index: '2d'  // create the geospatial index 
}, 
yakName:String, 
yakDescription:String, 
yakPhone:String, 
yakOwner:String 
}); 

module.exports = mongoose.model('Yak', YakSchema); 

和郵政路線

router.route('/yaks') 

.post(function(req, res) { 
    var yak = new Yak(); 

    //First, need to get lat, lng from the address 
    var addressString = req.body.city + ", " + req.body.street + ", " + req.body.state + ", " + req.body.postalcode; 
    geocoder.geocode(addressString, function (err, data) { 

     //error handling needed 
     if (err){ 
      res.status(500).json({ 
       status:500, 
       error:err 
      }); 
      res.end(); 
     } 

     var coord = data['results'][0]['geometry']['location']; 
     var lat = coord.lat; 
     var lng = coord.lng; 

     //Second, use the Model to save the yak with a geoJSON point 

     yak.catagory = req.body.catagory; 
     yak.loc = [lng, lat]; 
     yak.yakName = req.body.yakName; 
     yak.yakDescription = req.body.yakDescription; 
     yak.yakPhone = req.body.yakPhone; 
     yak.yakOwner = req.body.yakOwner; 
     yak.save(function (err) { 
      if (err) { 
       res.status(500); 
       res.json({ 
        status: 500, 
        error: err 
       }); 
       res.end(); 
      } 
      else { 
       res.json({ 
        status: 200, 
        yak: yak 
       }); 
       res.end(); 
      } 
     }); 
    }); 
}); 

在這裏,我S中的用戶模型和用戶的路線不工作

var mongoose = require('mongoose'); 
var Schema = mongoose.Schema; 


var UserSchema = new Schema({ 
username: { type: String, required: true, index: { unique: true } }, 
password: { type: String, required: true } 
}); 

module.exports = mongoose.model('User', UserSchema); 

和郵政路線

router.route('/users') 
    .post(function(req, res) { 


    console.log(req.body.username); 
    console.log(req.body.password); 


    var user = new User(); 
    user.username = req.body.username; 
    user.password = req.body.password; 

    console.log(user); 

    user.save(function(err){ 

     if (err) { 
      res.status(500); 
      res.json({ 
       status: 500, 
       error: err 
      }); 
      res.end(); 
     } 
     else { 
      res.json({ 
       status: 200, 
       user: user 
      }); 
      res.end(); 
     } 
    }); 
}); 

我看到的唯一區別是,保存爲「犛牛」路線法被包裹在一個回調對於先前執行的對地點進行地理編碼的方法。用戶路由不在回呼中。

這對我來說「感覺」與節點的異步本質有關,但是對於新節點我不確定。任何幫助是極大appreciuated

編輯*

這裏是在佈線後控制檯.LOG的結果,但在此之前保存。我在保存中添加了一個console.log,但它永遠不會被註銷...

以下是用戶名和密碼的console.log的結果INSIDE POST路徑但在保存之前。我沒加的console.log的保存方法,它從來沒有得到所謂的....

heroku[router]: at=info method=OPTIONS  path="/api/users" host=frozen-  
peak-5921.herokuapp.com request_id=6230237d-7adc-4a51-8a26-03da99f8b7e3  
fwd="74.202.146.157" dyno=web.1 connect=2ms service=14ms status=204 bytes=289 
app[web.1]: asd 
app[web.1]: [email protected] 
app[web.1]: { password: 'asd', 
app[web.1]: username: '[email protected]', 
app[web.1]: _id: 5612e098a7f49c1100000001 } 
heroku[router]: at=error code=H12 desc="Request timeout" method=POST 
path="/api/users" host=frozen-peak-5921.herokuapp.com request_id=f6c0fde8- 
c2e4-49e5-ad65-ba6afed3b731 fwd="74.202.146.157" dyno=web.1 connect=1ms 
service=30001ms status=503 byte 
+0

你不需要使用,因爲res.json res.end()()()內部調用res.end。你可以在你的user.save函數中添加一些console.log()來查看是否有任何錯誤或者它是否到達那裏 – Molda

+0

回調不會有什麼區別,如果你試圖使用回調之外的結果。你看到用戶名和密碼記錄正確嗎? – piemonkey

+0

以下是用戶名和密碼的console.log結果INSIDE POST路徑但在保存之前。我沒有添加一個console.log保存方法,它從來沒有調用.... –

回答

0

OK,WOW ....所以我用在Heroku上一個MongoLab沙箱實例。 9月30日,MongoLab更新了他們所有的MongoDB沙箱實例。一旦我將我的package.json文件mongoose依賴項更改爲「*」,做了一個npm安裝以安裝最新版本的mongoose並重新部署到Heroku,它就可以工作。所以這是由於MongoDB版本和貓鼬版本之間不兼容造成的。

某種有意義的錯誤處理該效果會的是不錯的....