错误
ERROR Error: Uncaught (in promise): ReferenceError: fetch is not defined
ReferenceError: fetch is not defined
at toDataURL (D:\Github\CountYourDrinks\dist\Worth-a-shot\server\main.js:349225:28)
at D:\Github\CountYourDrinks\dist\Worth-a-shot\server\main.js:351030:88
at Array.forEach (<anonymous>)
at GametypeService.get gameTypes [as gameTypes] (D:\Github\CountYourDrinks\dist\Worth-a-shot\server\main.js:351028:28)
at GameTypesResolver.resolve (D:\Github\CountYourDrinks\dist\Worth-a-shot\server\main.js:349524:37)
at getResolver (D:\Github\CountYourDrinks\dist\Worth-a-shot\server\main.js:148646:59)
at MergeMapSubscriber.project (D:\Github\CountYourDrinks\dist\Worth-a-shot\server\main.js:148632:146)
at MergeMapSubscriber._tryNext (D:\Github\CountYourDrinks\dist\Worth-a-shot\server\main.js:321242:27)
at MergeMapSubscriber._next (D:\Github\CountYourDrinks\dist\Worth-a-shot\server\main.js:321232:18)
at MergeMapSubscriber.next (D:\Github\CountYourDrinks\dist\Worth-a-shot\server\main.js:316467:18)
at resolvePromise (D:\Github\CountYourDrinks\dist\Worth-a-shot\server\main.js:332672:39)
at resolvePromise (D:\Github\CountYourDrinks\dist\Worth-a-shot\server\main.js:332624:21)
at D:\Github\CountYourDrinks\dist\Worth-a-shot\server\main.js:332734:21
at ZoneDelegate../node_modules/zone.js/dist/zone-node.js.ZoneDelegate.invokeTask (D:\Github\CountYourDrinks\dist\Worth-a-shot\server\main.js:332261:35)
at Object.onInvokeTask (D:\Github\CountYourDrinks\dist\Worth-a-shot\server\main.js:85361:33)
at ZoneDelegate../node_modules/zone.js/dist/zone-node.js.ZoneDelegate.invokeTask (D:\Github\CountYourDrinks\dist\Worth-a-shot\server\main.js:332260:40)
at Zone../node_modules/zone.js/dist/zone-node.js.Zone.runTask (D:\Github\CountYourDrinks\dist\Worth-a-shot\server\main.js:332028:51)
at drainMicroTaskQueue (D:\Github\CountYourDrinks\dist\Worth-a-shot\server\main.js:332441:39)
at ./node_modules/zone.js/dist/zone-node.js.ZoneTask.invokeTask (D:\Github\CountYourDrinks\dist\Worth-a-shot\server\main.js:332347:25)
at Server.ZoneTask.invoke (D:\Github\CountYourDrinks\dist\Worth-a-shot\server\main.js:332332:52) {
rejection: ReferenceError: fetch is not defined
at toDataURL (D:\Github\CountYourDrinks\dist\Worth-a-shot\server\main.js:349225:28)
at D:\Github\CountYourDrinks\dist\Worth-a-shot\server\main.js:351030:88
at Array.forEach (<anonymous>)
at GametypeService.get gameTypes [as gameTypes] (D:\Github\CountYourDrinks\dist\Worth-a-shot\server\main.js:351028:28)
at GameTypesResolver.resolve (D:\Github\CountYourDrinks\dist\Worth-a-shot\server\main.js:349524:37)
at getResolver (D:\Github\CountYourDrinks\dist\Worth-a-shot\server\main.js:148646:59)
at MergeMapSubscriber.project (D:\Github\CountYourDrinks\dist\Worth-a-shot\server\main.js:148632:146)
at MergeMapSubscriber._tryNext (D:\Github\CountYourDrinks\dist\Worth-a-shot\server\main.js:321242:27)
at MergeMapSubscriber._next (D:\Github\CountYourDrinks\dist\Worth-a-shot\server\main.js:321232:18)
at MergeMapSubscriber.next (D:\Github\CountYourDrinks\dist\Worth-a-shot\server\main.js:316467:18),
promise: ZoneAwarePromise [Promise] {
__zone_symbol__state: 0,
__zone_symbol__value: ReferenceError: fetch is not defined
at toDataURL (D:\Github\CountYourDrinks\dist\Worth-a-shot\server\main.js:349225:28)
at D:\Github\CountYourDrinks\dist\Worth-a-shot\server\main.js:351030:88
at Array.forEach (<anonymous>)
at GametypeService.get gameTypes [as gameTypes] (D:\Github\CountYourDrinks\dist\Worth-a-shot\server\main.js:351028:28)
at GameTypesResolver.resolve (D:\Github\CountYourDrinks\dist\Worth-a-shot\server\main.js:349524:37)
at getResolver (D:\Github\CountYourDrinks\dist\Worth-a-shot\server\main.js:148646:59)
at MergeMapSubscriber.project (D:\Github\CountYourDrinks\dist\Worth-a-shot\server\main.js:148632:146)
at MergeMapSubscriber._tryNext (D:\Github\CountYourDrinks\dist\Worth-a-shot\server\main.js:321242:27)
at MergeMapSubscriber._next (D:\Github\CountYourDrinks\dist\Worth-a-shot\server\main.js:321232:18)
at MergeMapSubscriber.next (D:\Github\CountYourDrinks\dist\Worth-a-shot\server\main.js:316467:18)
},
zone: Zone {
_parent: Zone {
_parent: null,
_name: '<root>',
_properties: {},
_zoneDelegate: [ZoneDelegate]
},
_name: 'angular',
_properties: { isAngularZone: true, maybeDelayChangeDetection: false },
_zoneDelegate: ZoneDelegate {
_taskCounts: [Object],
zone: [Circular],
_parentDelegate: [ZoneDelegate],
_forkZS: null,
_forkDlgt: null,
_forkCurrZone: null,
_interceptZS: null,
_interceptDlgt: null,
_interceptCurrZone: null,
_invokeZS: [Object],
_invokeDlgt: [ZoneDelegate],
_invokeCurrZone: [Circular],
_handleErrorZS: [Object],
_handleErrorDlgt: [ZoneDelegate],
_handleErrorCurrZone: [Circular],
_scheduleTaskZS: [Object],
_scheduleTaskDlgt: [ZoneDelegate],
_scheduleTaskCurrZone: [Circular],
_invokeTaskZS: [Object],
_invokeTaskDlgt: [ZoneDelegate],
_invokeTaskCurrZone: [Circular],
_cancelTaskZS: [Object],
_cancelTaskDlgt: [ZoneDelegate],
_cancelTaskCurrZone: [Circular],
_hasTaskZS: [Object],
_hasTaskDlgt: [ZoneDelegate],
_hasTaskDlgtOwner: [Circular],
_hasTaskCurrZone: [Circular]
}
},
task: ZoneTask {
_zone: Zone {
_parent: [Zone],
_name: 'angular',
_properties: [Object],
_zoneDelegate: [ZoneDelegate]
},
runCount: 0,
_zoneDelegates: null,
_state: 'notScheduled',
type: 'microTask',
source: 'Promise.then',
data: ZoneAwarePromise [Promise] {
__zone_symbol__state: 0,
__zone_symbol__value: ReferenceError: fetch is not defined
at toDataURL (D:\Github\CountYourDrinks\dist\Worth-a-shot\server\main.js:349225:28)
at D:\Github\CountYourDrinks\dist\Worth-a-shot\server\main.js:351030:88
at Array.forEach (<anonymous>)
at GametypeService.get gameTypes [as gameTypes] (D:\Github\CountYourDrinks\dist\Worth-a-shot\server\main.js:351028:28)
at GameTypesResolver.resolve (D:\Github\CountYourDrinks\dist\Worth-a-shot\server\main.js:349524:37)
at getResolver (D:\Github\CountYourDrinks\dist\Worth-a-shot\server\main.js:148646:59)
at MergeMapSubscriber.project (D:\Github\CountYourDrinks\dist\Worth-a-shot\server\main.js:148632:146)
at MergeMapSubscriber._tryNext (D:\Github\CountYourDrinks\dist\Worth-a-shot\server\main.js:321242:27)
at MergeMapSubscriber._next (D:\Github\CountYourDrinks\dist\Worth-a-shot\server\main.js:321232:18)
at MergeMapSubscriber.next (D:\Github\CountYourDrinks\dist\Worth-a-shot\server\main.js:316467:18)
},
scheduleFn: undefined,
cancelFn: undefined,
callback: [Function],
invoke: [Function]
}
}
ERROR NetworkError
at XMLHttpRequest.send (D:\Github\CountYourDrinks\dist\Worth-a-shot\server\main.js:329773:19)
at Observable._subscribe (D:\Github\CountYourDrinks\dist\Worth-a-shot\server\main.js:28155:17)
at Observable._trySubscribe (D:\Github\CountYourDrinks\dist\Worth-a-shot\server\main.js:315923:25)
at Observable.subscribe (D:\Github\CountYourDrinks\dist\Worth-a-shot\server\main.js:315909:22)
at scheduleTask (D:\Github\CountYourDrinks\dist\Worth-a-shot\server\main.js:144497:32)
at Observable._subscribe (D:\Github\CountYourDrinks\dist\Worth-a-shot\server\main.js:144535:13)
at Observable._trySubscribe (D:\Github\CountYourDrinks\dist\Worth-a-shot\server\main.js:315923:25)
at Observable.subscribe (D:\Github\CountYourDrinks\dist\Worth-a-shot\server\main.js:315909:22)
at innerSubscribe (D:\Github\CountYourDrinks\dist\Worth-a-shot\server\main.js:316912:23)
at MergeMapSubscriber._innerSub (D:\Github\CountYourDrinks\dist\Worth-a-shot\server\main.js:321255:105)
我理解的问题
据我所知,服务器端功能并不总是支持前端库,在这种情况下,服务器不支持 fetch
API,所以我应该用具有相同功能的东西替换它。>
我可以删除 fetch
的所有功能,并且不会对我的代码产生太大影响。但是,这似乎是一个糟糕的解决方案,我相当确定我会在使用其他库或客户端 API(例如 window
和 localStorage
)时遇到此错误。
我的尝试
我已经阅读了所有我能找到的错误报告,但这并没有太大帮助。
我尝试将以下代码块添加到我的 server.ts
或 main.server.ts
文件中:
global['fetch'] = null;
或
export const fetch = require("node-fetch");`
或
const fetch = require("node-fetch");`
或
var fetch = require("node-fetch");`
或
import fetch from 'node-fetch';
但没有任何帮助。
我想我有些了解这个问题,但我不明白如何解决它。
额外,我不确定这是否有帮助
main.server.ts
import { enableProdMode } from '@angular/core';
import { environment } from './environments/environment';
if (environment.production) {
enableProdMode();
}
export { AppServerModule } from './app/app.server.module';
export { renderModule, renderModuleFactory } from '@angular/platform-server';
server.ts
import 'zone.js/dist/zone-node';
import { ngExpressEngine } from '@nguniversal/express-engine';
import * as express from 'express';
import { join } from 'path';
import { AppServerModule } from './src/main.server';
import { APP_BASE_HREF } from '@angular/common';
import { existsSync } from 'fs';
// The Express app is exported so that it can be used by serverless Functions.
export function app(): express.Express {
const server = express();
const distFolder = join(process.cwd(), 'dist/Worth-a-shot/browser');
const indexHtml = existsSync(join(distFolder, 'index.original.html')) ? 'index.original.html' : 'index';
// Our Universal express-engine (found @ https://github.com/angular/universal/tree/master/modules/express-engine)
server.engine('html', ngExpressEngine({
bootstrap: AppServerModule,
}));
server.set('view engine', 'html');
server.set('views', distFolder);
// Example Express Rest API endpoints
// server.get('/api/**', (req, res) => { });
// Serve static files from /browser
server.get('*.*', express.static(distFolder, {
maxAge: '1y'
}));
// All regular routes use the Universal engine
server.get('*', (req, res) => {
res.render(indexHtml, { req, providers: [{ provide: APP_BASE_HREF, useValue: req.baseUrl }] });
});
return server;
}
function run(): void {
const port = process.env.PORT || 4000;
// Start up the Node server
const server = app();
server.listen(port, () => {
console.log(`Node Express server listening on http://localhost:${port}`);
});
}
// Webpack will replace 'require' with '__webpack_require__'
// '__non_webpack_require__' is a proxy to Node 'require'
// The below code is to ensure that the server is run only when not requiring the bundle.
declare const __non_webpack_require__: NodeRequire;
const mainModule = __non_webpack_require__.main;
const moduleFilename = mainModule && mainModule.filename || '';
if (moduleFilename === __filename || moduleFilename.includes('iisnode')) {
run();
}
export * from './src/main.server';