我已经研究这个问题了好几个星期了,还没有发现任何结论。我正在完成一个教程,在创建新用户时遇到了问题[创建新的mongodb文档(可能还有子文档)]。让我先从堆栈开始。
基准是MongoDB Standalone,Express,NodeJS。构建此模块时,我使用了此处列出的每个模块的最新版本:
"async": "^3.1.0",
"bcryptjs": "^2.4.3",
"body-parser": "^1.19.0",
"bootstrap": "^4.3.1",
"connect-flash": "^0.1.1",
"cookie-parser": "~1.4.4",
"debug": "~2.6.9",
"express": "~4.16.1",
"express-handlebars": "*",
"express-messages": "^1.0.1",
"express-session": "^1.16.2",
"express-validator": "^5.3.1",
"http-errors": "~1.6.3",
"mongodb": "^3.3.2",
"mongoose": "^5.7.3",
"morgan": "~1.9.1",
"passport": "^0.4.0",
"passport-http": "^0.3.0",
"passport-local": "^1.0.0",
"serve-favicon": "^2.5.0"
当我从教程中运行项目文件时,我使用的是Mongoose的旧版本...低于v5.0。
我设置了几个陷阱,并将问题作为问题子项深入到了以下行:
// Create Student User
module.exports.saveStudent = function(newUser, newStudent, callback){
console.log('Made it here');
bcrypt.hash(newUser.password, 10, function(err, hash){
console.log('Made it to the hash');
if(err) throw errl
// Set hash
console.log('No errors');
newUser.password = hash;
console.log('Student is being saved!');
//************** Here's the Problem ****************
async.parallel([newUser.save, newStudent.save], callback);
console.log('handled async');
});
}
我从不进入最后一个console.log语句,因此我可以假定它从不处理异步。因此,我没有将记录添加到数据库中,而是将其作为错误:
Made it here POST /users/register 302 183.633 ms - 46 Made it to the hash No errors Student is being saved! /Users/zp/Projects/Fortis_PHP/chl/node_modules/mongoose/lib/model.js:433 if (this.$__.saving) {
^
TypeError: Cannot read property '$__' of undefined
at Model.save (/Users/zp/Projects/Fortis_PHP/chl/node_modules/mongoose/lib/model.js:433:12)
at eachfn (/Users/zp/Projects/Fortis_PHP/chl/node_modules/async/dist/async.js:2952:28)
at eachOfArrayLike (/Users/zp/Projects/Fortis_PHP/chl/node_modules/async/dist/async.js:504:13)
at eachOf (/Users/zp/Projects/Fortis_PHP/chl/node_modules/async/dist/async.js:555:16)
at awaitable(eachOf) (/Users/zp/Projects/Fortis_PHP/chl/node_modules/async/dist/async.js:208:32)
at awaitify (/Users/zp/Projects/Fortis_PHP/chl/node_modules/async/dist/async.js:2951:9)
at awaitable() (/Users/zp/Projects/Fortis_PHP/chl/node_modules/async/dist/async.js:208:32)
at Object.parallel (/Users/zp/Projects/Fortis_PHP/chl/node_modules/async/dist/async.js:3033:16)
at /Users/zp/Projects/Fortis_PHP/chl/models/user.js:53:15
at /Users/zp/Projects/Fortis_PHP/chl/node_modules/bcryptjs/dist/bcrypt.js:1353:21
at Immediate.next (/Users/zp/Projects/Fortis_PHP/chl/node_modules/bcryptjs/dist/bcrypt.js:1233:21)
at runCallback (timers.js:705:18)
at tryOnImmediate (timers.js:676:5)
at processImmediate (timers.js:658:5)
因此,我正在寻找关于异步故障以及如何保存记录的任何想法。
在我完成的研究中,我看到其他人也遇到类似的问题,这似乎是我能找到的最接近的答案。
https://github.com/Automattic/mongoose/issues/3889
有人有什么想法吗? @JohnnyHK,你知道吗?