在另一个函数中调用函数

时间:2017-11-16 10:08:47

标签: javascript function typescript server

我有一个函数addNewMessageNewThread来显示服务器上的公共讨论。我想将此数据用于我创建的另一个函数matrixClient.publicRooms(function(err, data) { console.log("Public Rooms: %s", JSON.stringify(data)); console.log("data", data.chunk[0].aliases[0]); this.addNewMessageNewThread({ 'id': 'paul', 'author': 'Paul Manip', 'body': ' ?' }); });

addNewMessageNewThread()

当我这样做时,他告诉我他不知道这个功能:

  

错误类型错误:无法读取属性' addNewMessageNewThread'的   未定义

当我在函数外部测试此函数时,它可以正常工作

/////////// EDIT //////////////

这是我的函数addNewMessageNewThread(objMessage: any): void { const newUser: User = new User(objMessage.author, objMessage.site); const newThread: Thread = new Thread(objMessage.id, [newUser],objMessage.title); const newMessage = new Message(objMessage); objMessage.date = moment().toDate(); newThread.arrayMessages.push(newMessage); newThread.messages = Observable.of(newThread.arrayMessages); newThread.lastMessage = newMessage; objMessage.thread = newThread; this.addThread(newThread); }

@angular/compiler-cli@4.4.6 requires typescript@'>=2.1.0 <2.4.0' but 2.4.0 was found instead.
Using this version can result in undefined behaviour and difficult to debug problems.

Please run the following command to install a compatible version of TypeScript.

    npm install typescript@'>=2.1.0 <2.4.0'

To disable this warning run "ng set warnings.typescriptMismatch=false".

** NG Live Development Server is listening on localhost:4200, open your browser on http://localhost:4200/ **
Date: 2017-11-16T09:52:15.693Z
Hash: f55af8b09fa2b7f58749
Time: 32705ms
chunk {inline} inline.bundle.js (inline) 5.79 kB [entry] [rendered]
chunk {main} main.bundle.js (main) 483 kB [initial] [rendered]
chunk {polyfills} polyfills.bundle.js (polyfills) 1.09 MB [initial] [rendered]
chunk {styles} styles.bundle.js (styles) 1.61 MB [initial] [rendered]
chunk {vendor} vendor.bundle.js (vendor) 14.7 MB [initial] [rendered]

WARNING in ./node_modules/abp-ng2-module/node_modules/@angular/core/esm5/core.js
6438:15-36 Critical dependency: the request of a dependency is an expression
    at ImportLazyContextDependency.getWarnings (C:\angular\AbpCore\3.3.2\angular\node_modules\webpack\lib\dependencies\ContextDependency.js:39:18)
    at Compilation.reportDependencyErrorsAndWarnings (C:\angular\AbpCore\3.3.2\angular\node_modules\webpack\lib\Compilation.js:703:24)
    at Compilation.finish (C:\angular\AbpCore\3.3.2\angular\node_modules\webpack\lib\Compilation.js:561:9)
    at applyPluginsParallel.err (C:\angular\AbpCore\3.3.2\angular\node_modules\webpack\lib\Compiler.js:506:17)
    at C:\angular\AbpCore\3.3.2\angular\node_modules\tapable\lib\Tapable.js:289:11
    at _addModuleChain (C:\angular\AbpCore\3.3.2\angular\node_modules\webpack\lib\Compilation.js:507:11)
    at processModuleDependencies.err (C:\angular\AbpCore\3.3.2\angular\node_modules\webpack\lib\Compilation.js:477:14)
    at _combinedTickCallback (internal/process/next_tick.js:73:7)
    at process._tickCallback (internal/process/next_tick.js:104:9)
 @ ./node_modules/abp-ng2-module/node_modules/@angular/core/esm5/core.js
 @ ./node_modules/abp-ng2-module/src/multi-tenancy/abp-multi-tenancy.service.ts
 @ ./src/shared/session/app-session.service.ts
 @ ./src/root.module.ts
 @ ./src/main.ts
 @ multi webpack-dev-server/client?http://0.0.0.0:0 ./src/main.ts

WARNING in ./node_modules/abp-ng2-module/node_modules/@angular/core/esm5/core.js
6458:15-102 Critical dependency: the request of a dependency is an expression
    at ImportLazyContextDependency.getWarnings (C:\angular\AbpCore\3.3.2\angular\node_modules\webpack\lib\dependencies\ContextDependency.js:39:18)
    at Compilation.reportDependencyErrorsAndWarnings (C:\angular\AbpCore\3.3.2\angular\node_modules\webpack\lib\Compilation.js:703:24)
    at Compilation.finish (C:\angular\AbpCore\3.3.2\angular\node_modules\webpack\lib\Compilation.js:561:9)
    at applyPluginsParallel.err (C:\angular\AbpCore\3.3.2\angular\node_modules\webpack\lib\Compiler.js:506:17)
    at C:\angular\AbpCore\3.3.2\angular\node_modules\tapable\lib\Tapable.js:289:11
    at _addModuleChain (C:\angular\AbpCore\3.3.2\angular\node_modules\webpack\lib\Compilation.js:507:11)
    at processModuleDependencies.err (C:\angular\AbpCore\3.3.2\angular\node_modules\webpack\lib\Compilation.js:477:14)
    at _combinedTickCallback (internal/process/next_tick.js:73:7)
    at process._tickCallback (internal/process/next_tick.js:104:9)
 @ ./node_modules/abp-ng2-module/node_modules/@angular/core/esm5/core.js
 @ ./node_modules/abp-ng2-module/src/multi-tenancy/abp-multi-tenancy.service.ts
 @ ./src/shared/session/app-session.service.ts
 @ ./src/root.module.ts
 @ ./src/main.ts
 @ multi webpack-dev-server/client?http://0.0.0.0:0 ./src/main.ts

ERROR in Error: Metadata version mismatch for module C:/angular/AbpCore/3.3.2/angular/node_modules/abp-ng2-module/node_modules/@angular/http/http.d.ts, found version 4, expected 3, resolving symbol ABP_HTTP_PROVIDER in C:/angular/AbpCore/3.3.2/angular/node_modules/abp-ng2-module/src/abp.module.ts, resolving symbol RootModule in C:/angular/AbpCore/3.3.2/angular/src/root.module.ts, resolving symbol RootModule in C:/angular/AbpCore/3.3.2/angular/src/root.module.ts
    at Error (native)
    at syntaxError (C:\angular\AbpCore\3.3.2\angular\node_modules\@angular\compiler\bundles\compiler.umd.js:1729:34)
    at simplifyInContext (C:\angular\AbpCore\3.3.2\angular\node_modules\@angular\compiler\bundles\compiler.umd.js:24979:23)
    at StaticReflector.simplify (C:\angular\AbpCore\3.3.2\angular\node_modules\@angular\compiler\bundles\compiler.umd.js:24991:13)
    at StaticReflector.annotations (C:\angular\AbpCore\3.3.2\angular\node_modules\@angular\compiler\bundles\compiler.umd.js:24418:41)
    at _getNgModuleMetadata (C:\angular\AbpCore\3.3.2\angular\node_modules\@angular\compiler-cli\src\ngtools_impl.js:138:31)
    at _extractLazyRoutesFromStaticModule (C:\angular\AbpCore\3.3.2\angular\node_modules\@angular\compiler-cli\src\ngtools_impl.js:109:26)
    at Object.listLazyRoutesOfModule (C:\angular\AbpCore\3.3.2\angular\node_modules\@angular\compiler-cli\src\ngtools_impl.js:53:22)
    at Function.NgTools_InternalApi_NG_2.listLazyRoutes (C:\angular\AbpCore\3.3.2\angular\node_modules\@angular\compiler-cli\src\ngtools_api.js:91:39)
    at AotPlugin._getLazyRoutesFromNgtools (C:\angular\AbpCore\3.3.2\angular\node_modules\@ngtools\webpack\src\plugin.js:241:66)
    at _donePromise.Promise.resolve.then.then.then.then.then (C:\angular\AbpCore\3.3.2\angular\node_modules\@ngtools\webpack\src\plugin.js:495:24)
    at process._tickCallback (internal/process/next_tick.js:109:7)

webpack: Failed to compile.

2 个答案:

答案 0 :(得分:4)

您应该传递上下文this

matrixClient.publicRooms(function(err, data) {
  console.log("Public Rooms: %s", JSON.stringify(data));
  console.log("data", data.chunk[0].aliases[0]);

  this.addNewMessageNewThread({
    'id': 'paul',
    'author': 'Paul Manip',
    'body': '  ?'
  });
}, this); 

或使用arrow function来使用父上下文:

matrixClient.publicRooms((err, data) => {
  console.log("Public Rooms: %s", JSON.stringify(data));
  console.log("data", data.chunk[0].aliases[0]);

  this.addNewMessageNewThread({
    'id': 'paul',
    'author': 'Paul Manip',
    'body': '  ?'
  });
}); 

答案 1 :(得分:0)

调用this.addNewMessageNewThread的Instrad尝试使用matrixClient.addNewMessageNewThread

this参考回调的上下文。

如果此addNewMessageNewThread不属于matrixClient(猜测),则在致电matrixClient.publicRooms()之前,确认:

var that = this;

在回调中使用that而不是this