我正在跟随Adam Bretz&amp ;;完全堆叠的JAVASCRIPT开发。科林J.伊里格。当我到达第8章时,我尝试插入在线mongo数据库时无法通过此错误。我是否需要使用旧版本的节点?我已经查看了堆栈交换,并且无法按照所有建议的修复程序使其工作... 这是我的错误:
TypeError: Cannot read property '_id' of undefined
at insertEmployees (/Users/kikocarisse/Desktop/NODESITES/chapter8/index.js:100:17)
at /Users/kikocarisse/Desktop/NODESITES/chapter8/index.js:195:5
at /Users/kikocarisse/Desktop/NODESITES/chapter8/index.js:79:7
at Function.<anonymous> (/Users/kikocarisse/Desktop/NODESITES/chapter8/node_modules/mongoose/lib/model.js:3369:16)
at /Users/kikocarisse/Desktop/NODESITES/chapter8/node_modules/mongoose/lib/model.js:1890:18
at /Users/kikocarisse/Desktop/NODESITES/chapter8/node_modules/mongoose/node_modules/async/internal/parallel.js:35:9
at /Users/kikocarisse/Desktop/NODESITES/chapter8/node_modules/mongoose/node_modules/async/node_modules/lodash/before.js:31:21
at iteratorCallback (/Users/kikocarisse/Desktop/NODESITES/chapter8/node_modules/mongoose/node_modules/async/eachOf.js:52:13)
at /Users/kikocarisse/Desktop/NODESITES/chapter8/node_modules/mongoose/node_modules/async/internal/onlyOnce.js:12:16
at /Users/kikocarisse/Desktop/NODESITES/chapter8/node_modules/mongoose/node_modules/async/internal/parallel.js:32:13
at apply (/Users/kikocarisse/Desktop/NODESITES/chapter8/node_modules/mongoose/node_modules/async/node_modules/lodash/_apply.js:15:25)
at /Users/kikocarisse/Desktop/NODESITES/chapter8/node_modules/mongoose/node_modules/async/node_modules/lodash/_overRest.js:32:12
at model.callbackWrapper (/Users/kikocarisse/Desktop/NODESITES/chapter8/node_modules/mongoose/lib/model.js:1865:11)
at next_ (/Users/kikocarisse/Desktop/NODESITES/chapter8/node_modules/mongoose/node_modules/hooks-fixed/hooks.js:89:34)
at fnWrapper (/Users/kikocarisse/Desktop/NODESITES/chapter8/node_modules/mongoose/node_modules/hooks-fixed/hooks.js:186:8)
at /Users/kikocarisse/Desktop/NODESITES/chapter8/node_modules/mongoose/lib/model.js:3369:16
这是我的代码:
"use strict";
var a = require('./a.js');
var b = new a(5);
var mongoose = require('./node_modules/mongoose');
var express = require('express');
var app = express();
var bodyParser = require('./node_modules/body-parser');
app.use(bodyParser.urlencoded({
extended: true
}));
app.route('/echo')
.all((req,res)=>{
let pars = (Object.keys(req.body).length > 0)?req.body:req.query;
res.send(pars);
});
var Schema = mongoose.Schema;
var db = mongoose.connection;
var dbUrl = 'mongodb://*****:******@******:*****/****';
var TeamSchema = new Schema({
name: {
type: String,
required: true
}
});
var Team = mongoose.model('Team', TeamSchema);
var EmployeeSchema = new Schema({
name: {
first: {
type: String,
required: true
},
last: {
type: String,
required: true
}
},
team: {
type: Schema.Types.ObjectId,
ref: 'Team'
},
image: {
type: String,
default: 'images/user.png'
},
address: {
lines: {
type: [String]
},
postal: {
type: String
}
}
});
var Employee = mongoose.model('Employee', EmployeeSchema);
db.on('error', function () {
console.log('there was an error communicating with the database');
});
function insertTeams (callback) {
Team.create([{
name: 'Product Development'
}, {
name: 'Dev Ops'
}, {
name: 'Accounting'
}], function (error, pd, devops, acct) {
if (error) {
return callback(error);
} else {
console.info('teams successfully added')
callback(null, pd, devops, acct);
}
});
}
function insertEmployees (pd, devops, acct, callback) {
Employee.create([{
name: {
first: 'John',
last: 'Adams'
},
team: pd._id,
address: {
lines: ['2 Lincoln Memorial Cir NW'],
postal: '20037'
}
}, {
name: {
first: 'Thomas',
last: 'Jefferson'
},
team: devops._id,
address: {
lines: ['1600 Pennsylvania Avenue', 'White House'],
postal: '20500'
}
}, {
name: {
first: 'James',
last: 'Madison'
},
team: acct._id,
address: {
lines: ['2 15th St NW', 'PO Box 8675309'],
postal: '20007'
}
}, {
name: {
first: 'James',
last: 'Monroe'
},
team: acct._id,
address: {
lines: ['1850 West Basin Dr SW', 'Suite 210'],
postal: '20242'
}
}], function (error, johnadams) {
if (error) {
return callback(error);
} else {
console.info('employees successfully added');
callback(null, {
team: pd,
employee: johnadams
});
}
})
}
function retrieveEmployee (data, callback) {
Employee.findOne({
_id: data.employee._id
}).populate('team').exec(function (error, result) {
if (error) {
return callback (error);
} else {
console.log('*** Single Employee Result ***');
console.dir(result);
callback(null, data);
}
});
}
function retrieveEmployees (data, callback) {
Employee.find({
'name.first': /J/i
}, function (error, results) {
if (error) {
return callback(error);
} else {
console.log('*** Multiple Employees Result ***')
console.dir(results);
callback(null, data);
}
});
}
function updateEmployee (first, last, data, callback) {
console.log('*** Changing names ***');
console.dir(data.employee);
var employee = data.employee;
employee.name.first = first;
employee.name.last = last
employee.save(function (error, result) {
if (error) {
return callback(error);
} else {
console.log('*** Changed name to Andrew Jackson ***');
console.log(result);
callback(null, data);
}
});
}
mongoose.connect(dbUrl, function (err) {
if (err) {
return console.log('there was a problem connecting to the database!' + err);
}
console.log('connected!');
insertTeams(function (err, pd, devops, acct) {
if (err) {
return console.log(err)
}
insertEmployees(pd, devops, acct, function (err, result) {
retrieveEmployee(result, function (err, result) {
retrieveEmployees(result, function (err, result) {
updateEmployee('Andrew', 'Jackson', result, function (err, result) {
if (err) {
console.error(err);
} else {
console.info('database activity complete')
}
db.close();
process.exit();
});
});
});
});
});
});
答案 0 :(得分:1)
在第100行,devops
变量为undefined
,这就是您收到错误的原因。请参阅insertTeams
函数,在创建团队时,您将首先获得devops值。
这可能就是为什么你的创建回调就像:function (error, pd, devops, acct)
但看到Mongoose docs,它会传递一个数组作为create的第二个参数。
试试这个:
function insertTeams (callback) {
Team.create([{
name: 'Product Development'
}, {
name: 'Dev Ops'
}, {
name: 'Accounting'
}], function (error, teams) {
if (error) {
console.log(error);
return callback(error);
} else {
console.info('teams successfully added')
callback(null, teams[0], teams[1], teams[2]);
}
});
}
请注意,现在它获得了一个数组,并且它回调了它的回调元素,还注意到我已经添加了一个日志,如果有错误,你可以看到它发生的位置。
此外,如果您阅读错误日志并指出错误发生的行内容,它会有所帮助,请注意我们这里没有行号,这使得查找更加困难错误。