我在Heroku上正在运行一个后端。客户端是一个react-app。 一切正常,直到发出特定路由器请求为止。
我已经阅读了许多有关在本地运行而不是在Heroku上运行的应用程序的信息。区别在于,我可以启动我的应用程序并正常运行,直到对服务器进行特定的后请求为止。
这是路线。 console.logs用于调试。我首先要检查用户之前没有对该特定答案进行投票,然后该用户才能投票。
router.post('/:qID/answers/:aID/vote-:dir',function(req, res, next) {
console.log(req.body, "THIS IS THE REQ.BODY");
console.log(req.params.aID);
if(req.params.dir.search(/^(up|down)$/) === -1) {
var err = new Error("Not found");
err.status = 404;
next(err);
} else {
req.vote = req.params.dir;
req.userId = req.body.userId;
next();
}
},
function(req, res, next){
console.log(req.body, "IS THIS THE SAME VALUE?");
try{
User.findById(req.userId, (err, user) => {
if(err){
console.log(err, "IS THIS AN ERROR?");
next(err);
}
if(user.answersVoted.length > 0) {
for(let i = 0; i < user.answersVoted.length; i++){
if (user.answersVoted[i] == req.answer._id){
req.answered = true;
} else {
req.answered = false;
}
} return next();
} else {
updateAnswerArrayandVote(req, res);
}
});
}catch(err){
console.log(err, "IS THIS AN ERROR TOO?");
return next(err);
}
}, function(req, res, next){
if(req.answered){
res.status(422).json({message: "Only vote once!"});
} else {
updateAnswerArrayandVote(req, res);
}
});
这是客户端
handleVote = (id, dir) => {
if (dir === "up") {
axios.post(`https://sleepy-falls-59530.herokuapp.com/questions/${this.state.id}/answers/${id}/vote-up`, {
userId: this.props.userId
})
.then(res => {console.log(res)})
.catch(err => {
console.log(err);
this.setState({
show: true
})
})
} else {
axios.post(`https://sleepy-falls-59530.herokuapp.com/questions/${this.state.id}/answers/${id}/vote-down`, {
userId: this.props.userId
}).then(res => {console.log(res)})
.catch(err => {
console.log(err);
this.setState({
show: true
})
})
}
}
这是heroku日志中的错误消息:
]: throw er; // Unhandled 'error' event
2019-07-09T16:04:09.523006+00:00 app[web.1]: ^
2019-07-09T16:04:09.523009+00:00 app[web.1]:
2019-07-09T16:04:09.523012+00:00 app[web.1]: TypeError: Cannot read property 'length' of undefined
2019-07-09T16:04:09.523015+00:00 app[web.1]: at User.findById (/app/routes.js:329:34)
2019-07-09T16:04:09.523017+00:00 app[web.1]: at /app/node_modules/mongoose/lib/model.js:4645:16
2019-07-09T16:04:09.523019+00:00 app[web.1]: at /app/node_modules/mongoose/lib/query.js:4004:12
2019-07-09T16:04:09.523023+00:00 app[web.1]: at process.nextTick (/app/node_modules/mongoose/lib/query.js:2622:28)
2019-07-09T16:04:09.523025+00:00 app[web.1]: at _combinedTickCallback (internal/process/next_tick.js:132:7)
2019-07-09T16:04:09.523027+00:00 app[web.1]: at process._tickCallback (internal/process/next_tick.js:181:9)
2019-07-09T16:04:09.569320+00:00 app[web.1]: npm ERR! code ELIFECYCLE
2019-07-09T16:04:09.570120+00:00 app[web.1]: npm ERR! errno 1
2019-07-09T16:04:09.573462+00:00 app[web.1]: npm ERR! restAPI@1.0.0 start: `node app.js`
2019-07-09T16:04:09.573822+00:00 app[web.1]: npm ERR! Exit status 1
2019-07-09T16:04:09.574525+00:00 app[web.1]: npm ERR!
似乎User.findById()在heroku中不起作用,因为未定义用户对象上的answersVoted数组。这在本地如何运作,更重要的是,我该如何解决?我知道我正在发送正确的值,因为console.logs显示在日志中(尽管本文中未包括)。预先感谢!
编辑
所以heroku日志给了我这个
MuPuLM8q9i6A1a3uFvUzD8K2EbsGkaRt.QlHmBlMi3ry5ZK',
2019-07-10T18:52:49.981062+00:00 app[web.1]: __v: 1,
2019-07-10T18:52:49.981064+00:00 app[web.1]: answersVoted:
2019-07-10T18:52:49.981066+00:00 app[web.1]: [ '5d0bed720498750dbcc31b3e',
2019-07-10T18:52:49.981067+00:00 app[web.1]: '5ceac5948761ba2b4c2f7a7e',
2019-07-10T18:52:49.981069+00:00 app[web.1]: '5d0bed720498750dbcc31b3e',
2019-07-10T18:52:49.981072+00:00 app[web.1]: '5d17524e4dfec60016a07609',
2019-07-10T18:52:49.981074+00:00 app[web.1]: '5d0bed720498750dbcc31b3e',
2019-07-10T18:52:49.981076+00:00 app[web.1]: '5d17524e4dfec60016a07609',
2019-07-10T18:52:49.981078+00:00 app[web.1]: '5cf90359990b3233a0fbb1ce',
2019-07-10T18:52:49.981080+00:00 app[web.1]: '5d17524e4dfec60016a07609' ] } 'THE USER'
2019-07-10T18:52:49.981088+00:00 app[web.1]: THE USER IS A object
2019-07-10T18:52:49.981159+00:00 app[web.1]: undefined 'THE USERS ANSWERSARRAY'
2019-07-10T18:52:49.983370+00:00 app[web.1]: TypeError: Cannot read property 'length' of undefined
其代码为:
User.findById(req.userId, async (err, user) => {
console.log(user, "THE USER");
console.log("THE USER IS A", typeof user);
console.log(user.answersVoted, "THE USERS ANSWERSARRAY");
如您所见,我能够使用显示的AnswersVoted-array访问用户对象。
console.log(user)
向我显示用户和所有嵌套对象,包括AnswersVoted-array
console.log(user.answersVoted)
未定义。
异步错误或与heroku有关的错误(因为它与邮递员一起工作)?请帮忙。
答案 0 :(得分:0)
始终记住,如果以后要使用它,例如在快速路由器中,请将该属性实际添加到您的Mongoose模型的架构中。