当我学习nodejs时,我编写了lambda代码
UserSchema.static('findById', (id, callback) => {
console.log(this)
return this.find({id:id}, callback);
});
但是此程序发生了未找到find()的错误,日志如下 (我用'...'压缩了日志)
{ console: [Getter],
...
... 17 more items ],
versions:
{ http_parser: '2.8.0',
**node: '8.11.1'**,
v8: '6.2.414.50',
uv: '1.19.1',
zlib: '1.2.11',
ares: '1.10.1-DEV',
modules: '57',
nghttp2: '1.25.0',
openssl: '1.0.2o',
icu: '60.1',
unicode: '10.0',
cldr: '32.0',
tz: '2017c' },
arch: 'x64',
platform: 'win32',
release:
{ name: 'node',
lts: 'Carbon',
sourceUrl: 'https://nodejs.org/download/release/v8.11.1/node-v8.11.1.tar.gz',
headersUrl: 'https://nodejs.org/download/release/v8.11.1/node-v8.11.1-headers.tar.gz',
libUrl: 'https://nodejs.org/download/release/v8.11.1/win-x64/node.lib' },
argv:
[ 'C:\\Program Files\\nodejs\\node.exe',
'D:\\Study\\nodejs\\nodejs_httpserver_mongodb.js' ],
execArgv: [],
env:
{
...
OS: 'Windows_NT',
Path: 'C:\\Program Files (x86)\\Common Files\\Oracle\\Java\\javapath;C:\\oraclexe\\app\\oracle
...
mainModule:
Module {
id: '.',
exports: {},
parent: null,
filename: 'D:\\Study\\Node_MySQL_Spring_JSP_Eclipse\\nodejs\\nodejs_httpserver_mongodb.js',
loaded: true,
children: [Array],
paths: [Array] } },
Buffer:
{ …}
我想那是Node对象。
我将lambda代码更改为这样的功能
UserSchema.static('findById', function(id, callback){
console.log(this);
return this.find({id:id}, callback);});
然后,此代码成功完成,日志为
{ [Function: model]
hooks:
Kareem {
_pres: Map { 'save' => [Array], 'remove' => [Array] },
_posts: Map { 'init' => [Array], 'save' => [Array] } },
base:
Mongoose {
connections: [ [Object] ],
…
discriminators: undefined,
'$appliedMethods': true,
findById: [Function],
findAll: [Function],
'$appliedHooks': true,
schema:
Schema {
…
_updateForExec: [Function],
_ensurePath: [Function],
_validate: [Function] } },
'$__insertMany': [Function],
'$init': Promise { null, catch: [Function] } }
我想这是猫鼬模块中包含find()的对象。
函数参数和lambda函数之间的区别是什么。 什么时候可以使用lambda函数?