所以我正在学校的一个项目上工作,并且我负责与另一位同学一起为mongodb保存webhook从github的API到mongoose。听起来很简单吧? PFF。
使用样本数据时,它工作得很好,保存在JSON文件中。测试结果很好,所以一切都很好。但是当我们将我们的部分与其他人的工作合并时,即不是使用JSON文件为每个注释创建一个对象(issueCommentObject),数据库开始以特殊方式运行,首先保存初始注释,然后是初始注释评论和第二条评论(共3条),等等。我重复一遍。但是每个插页都有新的ID。
我们已经记录了所有可能的变量,花了几个小时进行调试,搜索网络却无法让它表现得与众不同。更糟糕的是,我们明天开会,我们应该演示本次和之前的迭代工作。请帮助我们!
- 代码段 -
在索引中
app.post('/', function(req, res){
if(req.body.action == issueComment)
{
console.log("Issue COMMENTED");
let issueCommentObject = {};
console.log(issueCommentObject)
issueCommentObject.action = issueComment;
issueCommentObject.issue_title = req.body.issue.title; ` ` //Title of issue
issueCommentObject.issue_owner = req.body.issue.user.login; ` `//Owner of issue
issueCommentObject.issue_status = req.body.issue.state; ` ` //State of issue
issueCommentObject.issue_milestone = req.body.issue.milestone; //No milestone = 0 null
issueCommentObject.issue_totalComments = req.body.issue.comments; ` ` //Total comments
issueCommentObject.issue_commentBody = req.body.comment.body; ` ` //The actual comments value
issueCommentObject.issue_commenter = req.body.sender.login; ` ` ` `//Who did the comment.
issueCommentObject.organization = req.body.organization.login;
//console.log(req.body)
responseHandler.saveIssueComment(issueCommentObject)
在responseHandler中:
saveIssue: function (issueJSON) {
return new Promise(function(resolve, reject){
responseHandler.dbHandler.handle(function () {
var newIssue = responseHandler.issuesModel(issueJSON);
if(newIssue.issue_title){
newIssue.save(function(err) {
if(err) resolve("testSaveIssueWithValid responded with - " +err);
console.log("issue saved".green)
resolve("issue saved");
});
}
else{
console.log("Wrong input".red)
resolve("issue not saved");
}
//responseHandler.dbHandler.disconnect()
});
})
},
在db open上调用作为param发送到dbhandler的函数:
handle: function(callback)
{
var db = dbHandler.mongoose.connection;
console.log("db.readyState: "+ db.readyState)
//if disconnected, connect to the database
if(db.readyState == 0)
{
console.log("Connectin hoj".rainbow)
dbHandler.connect();
}
console.log("db.readyState: "+db.readyState)
db.on('error', console.error.bind(console, 'connection error:'));
// If the Node process ends, close the Mongoose connection
..
db.on('open', callback);
}
/*
* connects if mongoose connection
* @return void
*/
connect: function()
{
console.log("conn")
var options = {
server: {
//keepalive in milliseconds
//http://mongoosejs.com/docs/connections.html
options: { keepAlive: 3000 }
},
}
dbHandler.mongoose.connect(dbHandler.dbHost);
},
disconnect: function () {
dbHandler.mongoose.disconnect()
console.log("responseHandler.dbHandler.mongoose.disconnect() ed")
// body...
}
问题的架构模型
var responseSchema = new Schema({
name: String,
organisation: String,
repo: String,
response: String
});
var response = mongoose.model('response', responseSchema);