UnhandledPromiseRejectionWarning: TypeError: this.second() is not a function
UnhandledPromiseRejectionWarning: TypeError: this.second() is not a function
我在将数据附加到 csv 文件时遇到了一些问题。
首先使用 cheerio
从网页中检索数据。但是当我想执行一个函数来检查文件exists/accessible时,我不能运行class中的函数。即使我将数据传递给第三个函数writeDataIntoFile()
,它仍然显示相同的错误。
代码如下:
import request = require('request');
import cheerio = require('cheerio');
import converter = require('json-2-csv');
import fs = require('fs');
export class TidalDataService {
private readonly siteURL = "webpage.url";
private readonly filepath = 'data.csv';
public data = "";
public async firstExecuted() {
request(this.siteURL, async function (err, res, body) {
if (!err) {
let $ = cheerio.load(body);
var data = [];
var dateIndex = [];
// Crawl data from page...
console.log(data);
try {
var isHeaderExist = this.isHeader(); // here
}
catch (err) {
console.log("ol21n31o2i3n123");
throw err;
}
try{
this.writeDataIntoFile(data, isHeaderExist); // and here
}
catch (err) {
throw err;
}
}
});
}
public async isHeader(): Promise<boolean> {
return new Promise((resolve, reject) => {
fs.access(this.filepath, fs.constants.F_OK | fs.constants.W_OK, (err) => {
if (err)
reject(err);
resolve(true);
// console.log(`${filepath} ${err ? 'does not exist' : 'exists'}`);
});
});
}
public async writeDataIntoFile(data: object[], isHeaderExist: boolean) {
converter.json2csv(data, (err, csv) => {
if (err) {
throw err;
}
// print CSV string
console.log(csv);
// check if file is accessible/exists
if (isHeaderExist) {
console.log(`${this.filepath} ${err ? 'does not exist' : 'exists'}`);
// write CSV to a file
fs.writeFile(this.filepath, csv, (err) => {
if (err) throw err;
});
}
else {
// csv.shift();
fs.appendFile(this.filepath, csv, (err) => {
if (err) throw err;
});
}
});
}
}
这是错误堆栈跟踪:
(node:7976) UnhandledPromiseRejectionWarning: TypeError: this.writeDataIntoFile is not a function
[0] at Request.<anonymous> (C:\Users\user\Projects\project\dist\modules\module123\module123.service.js:55:34)
[0] at Generator.next (<anonymous>)
[0] at C:\Users\user\Projects\project\dist\modules\module123\module123.service.js:8:71
[0] at new Promise (<anonymous>)
[0] at __awaiter (C:\Users\user\Projects\project\dist\modules\module123\module123.service.js:4:12)
[0] at Request._callback (C:\Users\user\Projects\project\dist\modules\module123\module123.service.js:26:24)
[0] at Request.self.callback (C:\Users\user\Projects\project\node_modules\request\request.js:185:22)
[0] at Request.emit (events.js:400:28)
[0] at Request.<anonymous> (C:\Users\user\Projects\project\node_modules\request\request.js:1154:10)
[0] at Request.emit (events.js:400:28)
[0] (node:7976) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 2)
问题是您使用了 function
,它遮蔽了 this
。
使用箭头函数。
变化自
public async firstExecuted() {
// `this` is shadowed
request(this.siteURL, async function (err, res, body) {
到
public async firstExecuted() {
request(this.siteURL, async (err, res, body) => {
我在将数据附加到 csv 文件时遇到了一些问题。
首先使用 cheerio
从网页中检索数据。但是当我想执行一个函数来检查文件exists/accessible时,我不能运行class中的函数。即使我将数据传递给第三个函数writeDataIntoFile()
,它仍然显示相同的错误。
代码如下:
import request = require('request');
import cheerio = require('cheerio');
import converter = require('json-2-csv');
import fs = require('fs');
export class TidalDataService {
private readonly siteURL = "webpage.url";
private readonly filepath = 'data.csv';
public data = "";
public async firstExecuted() {
request(this.siteURL, async function (err, res, body) {
if (!err) {
let $ = cheerio.load(body);
var data = [];
var dateIndex = [];
// Crawl data from page...
console.log(data);
try {
var isHeaderExist = this.isHeader(); // here
}
catch (err) {
console.log("ol21n31o2i3n123");
throw err;
}
try{
this.writeDataIntoFile(data, isHeaderExist); // and here
}
catch (err) {
throw err;
}
}
});
}
public async isHeader(): Promise<boolean> {
return new Promise((resolve, reject) => {
fs.access(this.filepath, fs.constants.F_OK | fs.constants.W_OK, (err) => {
if (err)
reject(err);
resolve(true);
// console.log(`${filepath} ${err ? 'does not exist' : 'exists'}`);
});
});
}
public async writeDataIntoFile(data: object[], isHeaderExist: boolean) {
converter.json2csv(data, (err, csv) => {
if (err) {
throw err;
}
// print CSV string
console.log(csv);
// check if file is accessible/exists
if (isHeaderExist) {
console.log(`${this.filepath} ${err ? 'does not exist' : 'exists'}`);
// write CSV to a file
fs.writeFile(this.filepath, csv, (err) => {
if (err) throw err;
});
}
else {
// csv.shift();
fs.appendFile(this.filepath, csv, (err) => {
if (err) throw err;
});
}
});
}
}
这是错误堆栈跟踪:
(node:7976) UnhandledPromiseRejectionWarning: TypeError: this.writeDataIntoFile is not a function
[0] at Request.<anonymous> (C:\Users\user\Projects\project\dist\modules\module123\module123.service.js:55:34)
[0] at Generator.next (<anonymous>)
[0] at C:\Users\user\Projects\project\dist\modules\module123\module123.service.js:8:71
[0] at new Promise (<anonymous>)
[0] at __awaiter (C:\Users\user\Projects\project\dist\modules\module123\module123.service.js:4:12)
[0] at Request._callback (C:\Users\user\Projects\project\dist\modules\module123\module123.service.js:26:24)
[0] at Request.self.callback (C:\Users\user\Projects\project\node_modules\request\request.js:185:22)
[0] at Request.emit (events.js:400:28)
[0] at Request.<anonymous> (C:\Users\user\Projects\project\node_modules\request\request.js:1154:10)
[0] at Request.emit (events.js:400:28)
[0] (node:7976) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 2)
问题是您使用了 function
,它遮蔽了 this
。
使用箭头函数。
变化自
public async firstExecuted() {
// `this` is shadowed
request(this.siteURL, async function (err, res, body) {
到
public async firstExecuted() {
request(this.siteURL, async (err, res, body) => {