如何在Express POSTpath中使用Mongoose保存方法

我是Express和Node的新手,有一个我不明白的问题。 我已经定义了两个不同的Express路由来为两个不同的Mongoose数据模型发送POST请求。 一个正常工作,正常返回,另一个在Heroku日志中返回204的状态,然后超时,就像保存甚至不执行…

这里是“牦牛”模型和相关的POST路线,它保存对象并将预期结果返回给我的ANgular客户端:

模型定义:

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(); } }); }); }); 

这是用户模型和用户路由不起作用

 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(); } }); }); 

我看到的唯一区别是,“牦牛”路线的保存方法被封装在一个先前执行的方法的callback中,该方法对位置进行地理编码。 用户路由不在回呼中。

这对我来说“感觉”与节点的asynchronous性质有关,但对于我来说是不确定的。 任何帮助都非常赞赏

编辑*

这是POST路由中的控制台.log的结果,但保存之前。 我在保存中添加了一个console.log,但它永远不会被注销…

这里是console.log的用户名和密码INSIDE POSTpath的结果,但保存之前。 我没有添加一个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]: me@mail.com app[web.1]: { password: 'asd', app[web.1]: username: 'me@mail.com', 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 

OK,WOW ….所以我在Heroku上使用了一个MongoLab沙箱实例。 9月30日,MongoLab更新了他们所有的MongoDB沙箱实例。 一旦我改变我的package.json文件mongoose依赖到“*”,做了一个npm的安装,以安装最新版本的mongoose,并重新部署到Heroku,这一切工作。 所以这是由于MongoDB版本与mongoose版本不兼容造成的。

一些有意义的error handling,以达到这个效果会很好….