- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
通过 Adam Bretz 和 Colin Ihrig 撰写的“使用 MEAN 进行全栈 JavaScript 开发”第 8 章中的示例以及书中的代码似乎并不完整(可能是故意的)。我花了很多时间调试谷歌和搜索 StackOverflow。该脚本可以正常执行,直到 insertEmployee 然后退出。我无法弄清楚 insertEmployee 参数(pd、devops、acct)的参数是如何设置的。我陷入了“回调 hell ”!
基本上,我使用 Node 来填充 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();
});
});
});
});
});
});
感谢 Node 和 Mongo 天才的帮助!我期待了解更多有关回调的知识
最佳答案
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);
}
});
}
这里的回调对我来说看起来很可疑。您将一个数组作为单个参数传递给 Team.create(),因此回调将被称为 function(err, results)
,其中 results 是包含插入文档的数组。
因此,当您像 callback(null, pd, devops, acct);
那样调用回调时,错误将为 null,pd 将是您的结果数组,devops 和 acct 将是未定义的。
您可以将团队作为单独的参数传递给 Team.create,然后也可以使用多个参数调用回调,或者保持原样并调整回调以处理数组。
或者这是“我已经好几天了,我只需要删除两个括号”版本(希望如此):
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);
}
});
}
一些放置得当的 console.log() 可以帮助您找出变量丢失的位置。
关于javascript - 我在 "TypeError: Cannot read property ' _id' of undefined 上绞尽脑汁好几天了”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38030139/
我只是有一个更琐碎的问题。 为什么undefined == undefined 返回true,而undefined >= undefined 为false? undefined 等于 undefine
用PHP 7.2编写套接字服务器。根据Firefox 60中的“网络”选项卡,服务器的一些HTTP响应的第一行随机变为undefined undefined undefined。因此,我尝试记录套接字
在 JavaScript 中这是真的: undefined == undefined 但这是错误的: undefined <= undefined 起初我以为<=运算符包含第一个,但我猜它试图将其转换
在回答这个问题 (Difference between [Object, Object] and Array(2)) 时,我在 JavaScript 数组中遇到了一些我以前不知道的东西(具有讽刺意味的
来自https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/of , Note: thi
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
当我添加 到我的 PrimeFaces Mobile 页面,然后我在服务器日志中收到以下警告 WARNING: JSF1064: Unable to find or serve resource, u
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
我是一名优秀的程序员,十分优秀!