UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'replace' of undefined
내 파일의 36 행에 오류가 발생 했습니다 index.js
(아래 스크립트 참조). Python 하위 프로세스에서 호출하고 있습니다.
참고 : 하위 프로세스를 통해 index.js를 호출해야합니다 .
이상한 이유로 프로그램은 20 %의 시간 동안 작동하지만이 오류가 계속 발생합니다. 이 오류를 어떻게 수정합니까?
main.py
import os
import subprocess
filePath = "/file/path/index.js"
command = subprocess.Popen(["node", filePath, "https://video.com/link"])
stdout, stderr = command.communicate()
command.wait()
오류
(node:64821) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'replace' of undefined
at /full/path/index.js:36:23
at processTicksAndRejections (internal/process/task_queues.js:93:5)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:64821) 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: 1)
(node:64821) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
index.js
const urlRegex = require("url-regex");
const fetch = require("node-fetch");
const fs = require("fs");
const https = require("https");
if (process.argv.length <= 2) {
console.log('Missing URL.');
console.log('Usage: node index.js <url>');
process.exit(0);
}
let url = process.argv[2];
if (url.endsWith('/')) url = url.substring(0, url.length - 1);
const urlParts = url.split("/");
let id = urlParts[urlParts.length - 1];
if (id.includes("?")) id = id.split("?")[0];
console.log(`URL > ${url}`);
console.log(`ID > ${id}`);
fetch(url)
.then(res => {
return res.text();
})
.then(body => {
const urls = body.match(urlRegex());
const baseUrl = urls.find(url => url.includes("playlist.m3u8"))
let liveUrl = baseUrl;
liveUrl = liveUrl.replace("pull-hls", "pull-flv");
liveUrl = liveUrl.split("/").slice(0, 5).join("/")
liveUrl += ".flv"
console.log(`Found live playlist (m3u8) URL: ${baseUrl}`);
console.log(`Found live flv URL: ${liveUrl}`);
console.log(`Writing live to output.flv. Press Ctrl C to STOP.`)
const file = fs.createWriteStream('output_path/output.flv')
https.get(liveUrl, function (response) {
response.pipe(file);
});
fs.writeFile('output_path/completion_check.txt', 'Hello World!', function (err) {
if (err) return console.log(err);
console.log('completion_check.txt has been created');
});
});
그 이유는 32 행에서 .find
함수가 반환 undefined
될 수 .replace
있고 undefined
값 에 대해 어떤 메서드 ( 이 경우)도 호출 할 수 없기 때문입니다 .
const baseUrl = urls.find(url => url.includes("playlist.m3u8"))
계속하기 전에이 줄 뒤에 확인을 추가하십시오. 예를 들면 :
if (baseUrl === undefined) {
console.log("Did not find any matching URLs")
return;
}
.find
작동 방식 에 대한 추가 정보 : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다