我是Express和Node的新手,有一个我不明白的问题。我定义了两个不同的Express路由,以对两个不同的Mongoose数据模型进行POST请求。一个工作正常并正确返回,另一个在Heroku日志中返回状态204,然后超时,就像保存甚至没有执行一样。
这是“ Yak”模型和关联的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);
和POST路线
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);
和POST路线
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();
}
});
});
我看到的唯一区别是,“ y牛”路线的save方法包装在对位置进行地理编码的先前执行的方法的回调中。用户路由不在回叫中。
我觉得这与节点的异步性质有关,但是我不确定它是新的。非常感谢任何帮助
编辑*
这是POST路由中控制台.log在保存之前的结果。我确实在保存中添加了console.log,但是它从未注销过...
这是用户名和密码的console.log在POST路由内但保存之前的结果。我确实将console.log添加到save方法,但它从未被调用...。
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
好,哇...。所以我在Heroku上使用MongoLab沙盒实例。9月30日,MongoLab更新了其所有MongoDB沙箱实例。将我的package.json文件猫鼬依赖关系更改为“ *”后,进行了npm安装以安装最新版本的猫鼬并重新部署到Heroku,这一切都可行。因此,这是由于MongoDB版本和Mongoose版本之间的不兼容引起的。
某种有意义的错误处理可以达到很好的效果。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句