我是 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();
}
});
});
我看到的唯一区别是“yak”路线的保存方法包装在先前执行的对位置进行地理编码的方法的回调中。用户路由不在回调中。
这对我来说“感觉”与 Node 的异步性质有关,但我不确定是新的。非常感谢任何帮助
编辑*
这是 POST 路由中但保存之前的控制台 .log 的结果。我确实在保存中添加了一个 console.log 但它永远不会被注销...
这是 POST 路由内但保存之前的用户名和密码的 console.log 结果。我确实将 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
最佳答案
好吧,哇......所以我在 Heroku 上使用 MongoLab 沙箱实例。 9 月 30 日,MongoLab 更新了所有 MongoDB 沙箱实例。一旦我将 package.json 文件 mongoose 依赖项更改为“*”,执行 npm install 来安装最新版本的 mongoose 并重新部署到 Heroku,一切正常。所以这是由于 MongoDB 版本和 mongoose 版本不兼容造成的。
某种有意义的错误处理效果会很好......
关于node.js - 如何在 Express POST 路由中使用 Mongoose 保存方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32957861/