我正在尝试使用Node.js运行此代码,但是我总是遇到相同的错误:
(node:9092) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 2): TypeError: Only absolute URLs are supported
(node:9092) [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.
我尝试了很多操作,例如粘贴整个路径或删除所有内容,但是我不知道,我的错误是什么。是否同时删除d3.csv(..)
方法或csvWriter
或两者都没关系。我测试了它是否带有then / catch或Promise,但没有任何变化。我尝试仅使用console.log("test");
运行一个简单的代码,并且此方法有效,因此不是Node.js无法正常工作。这是我的代码:
const fs = require('fs');
const d3 = require('d3');
global.fetch = require("node-fetch");
new Promise((resolve, reject) => {
d3.csv(".\\dat\\Meteorite_Landings.csv")}).then(function(data) {
//parse strings to numbers
data.forEach(function(d) {
var date;
if (d.year){
date = new Date (d.year);}
else{
date = null;
}
d["mass (g)"] = +d["mass (g)"];
d.reclat = +d.reclat;
d.reclong = +d.reclong;
d.date = date;
d.year = date? date.getYear() +1900 : null;
})
.catch((error) => {
assert.isNotOk(error);
});
console.log(data[0]);
createTimeLineData(data);
//createThemeRiver(data);
});
function filterYear(data){
/** Eliminate data without year */
filteredData = [];
data.forEach(function(item){
if(item.year){
filteredData.push(item);
}
});
return filteredData;
}
function getAllYears(data){
var yearArray=[];
data.forEach((item)=>{
/** creates array of all given years */
if(0 > yearArray.indexOf(item.year)){
yearArray.push(item.year);
}
});
return yearArray;
}
function createTimeLineData(rawData){
var data = filterYear(rawData);
var yearArray = (getAllYears(data)).sort();
csvWriter(yearArray);
}
function csvWriter(data){
const createCsvWriter = require("csv-writer").createObjectCsvWriter;
const csvWriter = createCsvWriter({path: 'trData.csv'});
new Promise((resolve, reject) => {
csvWriter.writeRecords(data)}).then(()=> console.log("success"))
.catch((error) => {
assert.isNotOk(error);
});
}
有人知道我在做什么错吗?
答案 0 :(得分:0)
您的代码似乎未按预期方式使用Promises。应使用Promise来包装异步操作,并应调用resolve()
或reject()
(并确定它是否进入调用该Promise的人的then()
或catch()
块)
使用异步代码时,一些最佳做法是始终返回函数期望的值(即使尤其是当它们是诺言时!),并减少catch
块的数量,以避免兑现诺言错误在答应链中被悄悄吞没。
考虑到这一点,请如下更改代码:
const fs = require('fs');
const d3 = require('d3');
global.fetch = require("node-fetch");
d3.csv(".\\dat\\Meteorite_Landings.csv")
.then(function(data) {
//parse strings to numbers
data.forEach(function(d) {
var date;
if (d.year){
date = new Date (d.year);}
else{
date = null;
}
d["mass (g)"] = +d["mass (g)"];
d.reclat = +d.reclat;
d.reclong = +d.reclong;
d.date = date;
d.year = date? date.getYear() +1900 : null;
});
return data;
}).then((data) => {
return createTimeLineData(data);
}).then(() => console.log("success"))
.catch((error) => {
assert.isNotOk(error);
});
function filterYear(data){
/** Eliminate data without year */
filteredData = [];
data.forEach(function(item){
if(item.year){
filteredData.push(item);
}
});
return filteredData;
}
function getAllYears(data){
var yearArray=[];
data.forEach((item)=>{
/** creates array of all given years */
if(0 > yearArray.indexOf(item.year)){
yearArray.push(item.year);
}
});
return yearArray;
}
function createTimeLineData(rawData){
var data = filterYear(rawData);
var yearArray = (getAllYears(data)).sort();
return csvWriter(yearArray);
}
function csvWriter(data){
const createCsvWriter = require("csv-writer").createObjectCsvWriter;
const csvWriter = createCsvWriter({path: 'trData.csv'});
return csvWriter.writeRecords(data);
}
更新:
错误消息中描述了真正破坏应用程序的行为:TypeError: Only absolute URLs are supported
,因此问题在于,读取CSV时您使用的是相对路径,而D3使用node-fetch
来读取CSV文件和节点-获取does not support relative URLs。
您可能想尝试使用path.resolve()
来包装URL,就像在d3.csv(path.resolve('/dat/Meteorite_Landings.csv'))
中一样(同样,不要忘记使用const path = require('path');
来要求使用路径)。