Adam Bretz &の "MEANを使った完全なスタックJavaScript開発"の第8章の例を使って作業してください。本書のコードはdoesnではありません(おそらく意図的に)完全ではないようだ。 IveはグーグルやStackOverflowのデバッグに多くの時間を費やしました。このスクリプトは、insertEmployeeが実行されてからbailsまで実行されます。私は、insertEmployee引数(pd、devops、acct)の引数がどのように設定されるかを理解できません。私は "コールバック地獄"に立ち往生!TypeError:未定義の '_id'プロパティを読み取ることができない
基本的に私はであり、ノードを使用してMongoDbを埋め込むです。
If I set the insertEmployee function to use pd._id for all the employees its fine, but using devops._id or acct._id always results in the error below
*TypeError: Cannot read property '_id' of undefined
at insertEmployees (/Users/Bluemagma/Sites/NodeJS Example Application/database/humanresourcesSchema.js:111:17)
at /Users/Bluemagma/Sites/NodeJS Example Application/database/humanresourcesSchema.js:199:3
at /Users/Bluemagma/Sites/NodeJS Example Application/database/humanresourcesSchema.js:62:4
at Function.<anonymous> (/Users/Bluemagma/Sites/NodeJS Example Application/node_modules/mongoose/lib/model.js:3352:16)
at /Users/Bluemagma/Sites/NodeJS Example Application/node_modules/mongoose/lib/model.js:1863:18
at /Users/Bluemagma/Sites/NodeJS Example Application/node_modules/async/lib/async.js:726:13
at /Users/Bluemagma/Sites/NodeJS Example Application/node_modules/async/lib/async.js:52:16
at done (/Users/Bluemagma/Sites/NodeJS Example Application/node_modules/async/lib/async.js:246:17)
at /Users/Bluemagma/Sites/NodeJS Example Application/node_modules/async/lib/async.js:44:16
at /Users/Bluemagma/Sites/NodeJS Example Application/node_modules/async/lib/async.js:723:17
at /Users/Bluemagma/Sites/NodeJS Example Application/node_modules/async/lib/async.js:167:37
at model.callbackWrapper (/Users/Bluemagma/Sites/NodeJS Example Application/node_modules/mongoose/lib/model.js:1841:11)
at next_ (/Users/Bluemagma/Sites/NodeJS Example Application/node_modules/hooks-fixed/hooks.js:89:34)
at fnWrapper (/Users/Bluemagma/Sites/NodeJS Example Application/node_modules/hooks-fixed/hooks.js:186:18)
at /Users/Bluemagma/Sites/NodeJS Example Application/node_modules/mongoose/lib/model.js:3352:16
at /Users/Bluemagma/Sites/NodeJS Example Application/node_modules/mongoose/lib/model.js:228:5*
humanresourcesSchema.js
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var db = mongoose.connection;
var dbUrl = 'mongodb://localhost/humanResources';
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 damn 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 sucessfully added sir!');
callback(null, pd, devops, acct);
}
});
}
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 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 sir!');
callback(null, {
team: pd,
employee: johnadams
});
}
})
}
function updateEmployee (first, last, data, callback) {
console.log('*** Changin 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);
}
});
}
function removeTeams() {
console.info("deleting all previously added teams sir!");
Team.remove({}, function(error, response) {
if(error) {
console.error("tried to delete all teams but " + error);
}
console.info("done deleting all teams sir!");
});
}
function removeEmployees() {
console.info("deleting all previously added employees sir!");
Employee.remove({}, function(error, response) {
if(error) {
console.error("tried to delete all employees but " + error);
}
console.info("done deleting all employees sir!");
});
}
mongoose.connect(dbUrl, function (err) {
if (err) {
return console.log('there was a problem connecting to the database sir!' + err);
}
console.log('connected to the database sir!');
removeTeams();
removeEmployees();
insertTeams(function (error, pd, devops, acct) {
if (error) {
return console.log(error);
}
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 sir!");
}
db.close();
process.exit();
});
});
});
});
});
});
ためHERESに私のコードは、あなたの助けのノードとMongoの天才をありがとう!私は、コールバックの詳細を学ぶのを楽しみにしています
私は最初にpd以外の引数を使っているImは "Team:devops._id"という行111を追加するべきです。 Team:pd._idにすべて設定しても問題はありません。 – bluemagma