TypeError.无法读取未定义的'$__'属性。无法读取未定义的'$__'属性 - Mongoose v5.^。

问题描述 投票:1回答:1

我认为这是async.js的问题,但我不知道为什么。

我已经研究这个问题好几个星期了,但还没有找到任何结论。 我正在研究一个教程,在创建一个新用户[创建一个新的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 语句,所以我可以假设它从来没有处理过 async. 所以我没有把记录添加到db中,而是得到这个错误。

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.comAutomatticmongooseissues3889。


谁有什么办法?@JohnnyHK,你知道吗?

node.js mongodb express mongoose async.js
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.