I am using c3 chart maker following the instructions of this github project: https://github.com/ashleydavis/nodejs-chart-rendering-example.
I would like to create a chart from a .csv data, however when i run the code below the chart is empty and I have this message: "Usage of deprecated field: 'series' ".
const c3ChartMaker = require('c3-chart-maker')
const inputFilePath = "./test_file.csv"
const chartDefinitionFile = "./chart.json"
const outputFilePath = "./chart.png"
c3ChartMaker(inputFilePath, chartDefinitionFile, outputFilePath)
.then(() => {
console.log('done')
})
.catch(err => {
console.error(err)
})
i test your code with this example: https://github.com/ashleydavis/c3-chart-maker
i use example-chart.json and example-data.csv in example folder and this is my code:
const c3ChartMaker = require('c3-chart-maker')
const inputFilePath = "./example-data.csv"
const chartDefinitionFile = "./example-chart.json"
const outputFilePath = "./example-js.png"
c3ChartMaker(inputFilePath, chartDefinitionFile, outputFilePath)
.then(() => {
console.log('done')
})
.catch(err => {
console.error(err)
})
when i run this code show me this image:
and on my console i have this message:
Usage of deprecated field: 'series'.
done
i read index.js code in node_modules\c3-chart-maker directory and find something:
if (chart.series) { // THIS SECTION IS DEPRECATED.
console.error("Usage of deprecated field: 'series'.");
if (!chart.data.columns) {
chart.data.columns = [];
}
var series = Object.keys(chart.series);
var dataFrame = new dataForge.DataFrame(data);
series.forEach(seriesName => {
var dataSeries = chart.series[seriesName];
if (Sugar.Object.isString(inputData) && seriesName !== "x") {
dataFrame = dataFrame.parseFloats(dataSeries).bake();
}
chart.data.columns.push(
[seriesName].concat(
dataFrame.getSeries(dataSeries)
.select(v => v === undefined ? null : v)
.toArray()
)
)
});
}
you see this error log: Usage of deprecated field: 'series'. because in example-chart.json file we have this line of json:
"series": {
"x": "Date",
"Close": "Close",
"Volume": "Volume"
},
and series is deprecated.
Related
I believe Plaid updated its createLinkToken documentation, but I can't seem to figure out what I'm doing wrong here. I'm taking a course, and here is the old code that worked in using a FirebaseFunction to create a link token with Plaid:
exports.createPlaidLinkToken = functions.https.onCall(async (data, context) => {
const customerId = context.auth.id;
const plaidClient = new plaid.Client({
clientID: functions.config().plaid.client_id,
secret: functions.config().plaid.secret,
env: plaid.environments.sandbox,
options: {
version: '2019-05-29',
},
});
return plaidClient.createLinkToken({
user: {
client_user_id: customerId,
},
client_name: "Bon Voyage",
products: ["auth"],
country_codes: ["US"],
language: "en"
}).then((apiResponse) => {
const linkToken = apiResponse.link_token;
return linkToken;
}).catch((err) => {
console.log(err);
throw new functions.https.HttpsError("internal", "Unable to create plaid link token: " + err);
});
});
I've tried a number of things. I know plaid.Client is now new.Configuration but I can't seem to figure out the rest. Any helpers?
You can see in the comments below what I've tried. I've modified the code as follows, and now receive Error status code 400.
const plaid = require('plaid');
const { Configuration, PlaidEnvironments, PlaidApi } = require("plaid");
exports.createPlaidLinkToken = functions.https.onCall(async (data, context) => {
const customerId = context.auth.uid;
const configuration = new Configuration({
basePath: PlaidEnvironments.sandbox,
baseOptions: {
headers: {
plaid_client_id: functions.config().plaid.client_id,
plaid_secret: functions.config().plaid.secret,
plaid_version: '2021-05-20'
},
},
});
const plaidClient = new PlaidApi(configuration);
return plaidClient.linkTokenCreate({
user: {
client_user_id: customerId,
},
client_name: "Bon Voyage",
products: ["auth"],
country_codes: ["US"],
language: "en"
})
.then((apiResponse) => {
const linkToken = apiResponse.data.link_token;
// const linkToken = response.link_token
return linkToken;
})
.catch((err) => {
console.log(err);
throw new functions.https.HttpsError(
"internal",
" Unable to create plaid link token: " + err
);
});
});
It's difficult to answer this question as you haven't mentioned what you've tried or what error you are experiencing. Have you reviewed the sample implementations in the docs that show how to do this, including the sample code in the Quickstart and Tiny Quickstart?
Off the top of my head, I do see that this sample code specifies an API version of 2019-05-29, which is not compatible with the latest version of the Node client library that uses new.Configuration.
Problem
Using 18next.t function to translate key, is getting me the generic sequelize unique constraint error message instead of defined custom message
Environment
sequelize#5.22.4
i18next#21.3.3
Model definition candidate.js
...
module.exports = (sequelize, DataTypes) => {
const Candidate = sequelize.define('Candidate', {
status: {
type: DataTypes.ENUM,
values: [
...
],
},
docTin : {
...
unique: {
args: 'candidates_unique_doctin_company_unity',
get msg() { return i18next.t('invalid-candidate-unique-doc-tin') }
},
...
Result:
docTin must be unique
Expected:
{Custom error message located on lang.json}
The solution that i founded was prototyping the main create function on model definition, to allow use functions on unique msg properties
...
const orgCreate = Candidate.create;
Candidate.create = function(){
return orgCreate
.apply(this, arguments)
.catch(err => {
const uniqueErrorName = 'SequelizeUniqueConstraintError'
if (err.name === uniqueErrorName) {
err.errors = err.errors.map(e => ({
...e,
message: typeof e.message === 'function' ? e.message() : e.message
}))
}
throw err;
});
...
replacing
get msg() { return i18next.t('invalid-candidate-unique-doc-tin') }
for
msg: () => i18next.t('invalid-candidate-unique-doc-tin')
I'm sorry, but I'm kinda new in this language.
I was creating a custom discord bot these days and I got stucked on this problem...
I gave this bot the possibility to load the commands dynamically from a folder with one module for each command, but now I was trying to make a command to reload them all, but each time after the commands are reloaded the output is always the same.
Here is the code:
refreshCommands = () => {
this.commands = {};
console.log("Refreshing commands");
Promise.all(fs.readdirSync("./dist/commands").map(file => {
return new Promise(async resolve => {
const tmp = (await import(`./commands/${file}`)).default;
this.commands[tmp.name] = tmp;
resolve(tmp);
});
})).then(() => {
console.log("Listing commands: ");
console.log(Object.keys(this.commands));
});
}
Of course I update the commands from the js file, and not from the ts 'cause I would have to compile it again.
I tried to make a simple "ping! Pong!" like command, and then to edit it to "ping! ping!" on runtime before using the //reload command, but it keeps writing "ping! Pong!"
Edit 1:
The modules I have to import are made like this one:
import command from "../utils/command";
import { Guild, GuildEmoji, GuildEmojiManager, Message, MessageEmbed, Role } from "discord.js";
import { games } from "../utils/games";
import app from "../app";
import ReactionListener from "../utils/reactionListener";
const roleMessage: command = {
name: "rolesMessage",
description: "",
execute: async (message, bot) => {
message.delete();
createRoles(message.guild as Guild);
const embed = new MessageEmbed()
.setColor('#F00')
.setTitle("React to set your ROLE!");
games.forEach(game => {
let emoji = message.guild?.emojis.cache.find(emoji => emoji.name === game.emoji);
console.log(emoji);
embed.fields.push({
name: game.name,
value: (emoji as GuildEmoji).toString(),
inline: false
});
});
const msg = await message.channel.send(embed);
app.reactionListeners.push(new ReactionListener(msg,
(reaction, user) => {
let tmp = games.find(game=> reaction.emoji.name === game.emoji);
if(tmp){
//msg.channel.send(tmp);
const role = (message.guild as Guild).roles.cache.find(role => role.name === tmp?.roleName) as Role;
message.guild?.members.cache.find(member => member.id === user.id)?.roles.add(role);
}else{
reaction.remove();
}
}, (reaction, user)=>{
let tmp = games.find(game=> reaction.emoji.name === game.emoji);
if(tmp){
//msg.channel.send(tmp);
const role = (message.guild as Guild).roles.cache.find(role => role.name === tmp?.roleName) as Role;
message.guild?.members.cache.find(member => member.id === user.id)?.roles.remove(role);
}
})
);
games.forEach(game => {
msg.react((message.guild?.emojis.cache.find(emoji => emoji.name === game.emoji) as GuildEmoji));
});
}
}
const createRoles = (guild: Guild) => {
games.forEach(game => {
if(!guild.roles.cache.find(role => role.name === game.roleName)){
guild.roles.create({
data: {
name: game.roleName,
color: "#9B59B6",
},
reason: 'we needed a role for Super Cool People',
})
.then(console.log)
.catch(console.error);
}
});
}
export default roleMessage;
This is a different one from the one I was talking about earlier, but the problem is the same... Once I update and reload it (from the js compiled version), the old version keeps being runned
I managed to find a solution to the problem.
As node js chaches every module once imported, I deleted it from the cache like this
refreshCommands = () => {
Promise.all(fs.readdirSync("./dist/commands").map(file => {
return new Promise(async resolve => {
delete require.cache[require.resolve('./commands/' + file)];
resolve(file);
});
})).then(() => {
this.commands = {};
console.log("Refreshing commands");
Promise.all(fs.readdirSync("./dist/commands").map(file => {
return new Promise(async resolve => {
const tmp = (await import(`./commands/${file}`)).default;
this.commands[tmp.name] = tmp;
resolve(tmp);
});
})).then(() => {
console.log("Listing commands: ");
console.log(Object.keys(this.commands));
});
});
}
The code might look like garbage, but it actually works... I'm on my way to make it better, but meanwhile I can rely on it.
Any suggestion is well accepted
I'm absolutely brand new to DynamoDb and I'm trying to simply write an object from a NodeJS Lambda. Based on what I've read and researched I should probably be using DocumentClient from the aws-sdk. I also found the following question here regarding issues with DocumentClient, but it doesn't seem to address my specific issue....which I can't really find/pinpoint unfortunately. I've set up a debugger to help with SAM local development, but it appears to be only providing some of the errors.
The code's implementation is shown here.
var params = {
TableName: "March-Madness-Teams",
Item: {
"Id": {"S": randstring.generate(9)},
"School":{"S": team_name},
"Seed": {"S": seed},
"ESPN_Id": {"S": espn_id}
}
}
console.log(JSON.stringify(params))
dynamodb.put(params, (error,data) => {
if (error) {
console.log("Error ", error)
} else {
console.log("Success! ", data)
}
})
Basically I'm scrubbing a website utilizing cheerio library and cherry picking values from the DOM and saving them into the json object shown below.
{
"TableName": "March-Madness-Teams",
"Item": {
"Id": {
"S": "ED311Oi3N"
},
"School": {
"S": "BAYLOR"
},
"Seed": {
"S": "1"
},
"ESPN_Id": {
"S": "239"
}
}
}
When I attempt to push this json object to Dynamo, I get errors says
Error MultipleValidationErrors: There were 2 validation errors:
* MissingRequiredParameter: Missing required key 'TableName' in params
* MissingRequiredParameter: Missing required key 'Item' in params
The above error is all good in well....I assume it didn't like the fact that I had wrapped those to keys in strings, so I removed the quotes and sent the following
{
TableName: "March-Madness-Teams",
Item: {
"Id": {
"S": "ED311Oi3N"
},
"School": {
"S": "BAYLOR"
},
"Seed": {
"S": "1"
},
"ESPN_Id": {
"S": "239"
}
}
}
However, when I do that...I kind of get nothing.
Here is a larger code snippet.
return new Promise((resolve,reject) => {
axios.get('http://www.espn.com/mens-college-basketball/bracketology')
.then(html => {
const dynamodb = new aws.DynamoDB.DocumentClient()
let $ = cheerio.load(html.data)
$('.region').each(async function(index, element){
var preregion = $(element).children('h3,b').text()
var region = preregion.substr(0, preregion.indexOf('(') - 1)
$(element).find('a').each(async function(index2, element2){
var seed = $(element2).siblings('span.rank').text()
if (seed.length > 2){
seed = $(element2).siblings('span.rank').text().substring(0, 2)
}
var espn_id = $(element2).attr('href').split('/').slice(-2)[0]
var team_name = $(element2).text()
var params = {
TableName: "March-Madness-Teams",
Item: {
"Id": randstring.generate(9),
"School":team_name,
"Seed": seed,
"ESPN_Id": espn_id
}
}
console.log(JSON.stringify(params))
// dynamodb.put(params)
// .then(function(data) {
// console.log(`Success`, data)
// })
})
})
})
})
Can you try without the type?
Instead of
"School":{"S": team_name},
for example, use
"School": team_name,
From your code, I can see the mis promise on the dynamodb request. Try to change your lines :
dynamodb.put(params).then(function(data) {
console.log(`Success`, data)
})
to be :
dynamodb.put(params).promise().then(function(data) {
console.log(`Success`, data)
})
you can combine with await too :
await dynamodb.put(params).promise().then(function(data) {
console.log(`Success`, data)
})
exports.lambdaHandler = async (event, context) => {
const html = await axios.get('http://www.espn.com/mens-college-basketball/bracketology')
let $ = cheerio.load(html.data)
const schools = buildCompleteSchoolObject(html, $)
try {
await writeSchoolsToDynamo(schools)
return { statusCode: 200 }
} catch (error) {
return { statusCode: 400, message: error.message }
}
}
const writeSchoolsToDynamo = async (schools) => {
const promises = schools.map(async school => {
await dynamodb.put(school).promise()
})
await Promise.all(promises)
}
const buildCompleteSchoolObject = (html, $) => {
const schools = []
$('.region').each(loopThroughSubRegions(schools, $))
return schools
}
const loopThroughSubRegions = (schools, $) => {
return (index, element) => {
var preregion = $(element).children('h3,b').text()
var region = preregion.substr(0, preregion.indexOf('(') - 1)
$(element).find('a').each(populateSchoolObjects(schools, $))
}
}
const populateSchoolObjects = (schools, $) => {
return (index, element) => {
var seed = $(element).siblings('span.rank').text()
if (seed.length > 2) {
seed = $(element).siblings('span.rank').text().substring(0, 2)
}
var espn_id = $(element).attr('href').split('/').slice(-2)[0]
var team_name = $(element).text()
schools.push({
TableName: "March-Madness-Teams",
Item: {
"Id": randstring.generate(9),
"School": team_name,
"Seed": seed,
"ESPN_Id": espn_id
}
})
}
}
I know this is drastically different from what I started with but I did some more digging and kind of kind of worked to this...I'm not sure if this is the best way, but I seemed to get it to work...Let me know if something should change!
Oh I understand what you want.
Maybe you can see the code above works, but there is one concept you have to improve here about async - await and promise especially on lambda function.
I have some notes here from your code above, maybe can be your consideration to improve your lambda :
Using await for every promise in lambda is not the best approach because we know the lambda time limitation. But sometimes we can do that for other case.
Maybe you can change the dynamodb.put method to be dynamodb.batchWriteItem :
The BatchWriteItem operation puts or deletes multiple items in one or more tables.
Or If you have to use dynamodb.put instead, try to get improve the code to be like so :
const writeSchoolsToDynamo = async (schools) => {
const promises = schools.map(school => {
dynamodb.put(school).promise()
})
return Promise.all(promises)
}
I have excel sheet called sampledata.xlsx which i converted into json and console.log to print this data.
server.js
var xlsx2json = require('xlsx2json')
xlsx2json(
'sampledata.xlsx',
{
dataStartingRow: 2,
mapping: {
'name': 'B',//name
'sku': 'C',//unit price //sku
'quantity': 'D',//quantity
}
}).then(jsonArray => {
// [
// {"col_1": "Barton LCC", "col_2": "30", "col_3": "86.69"}
// ]
//console.log(jsonArray);
});
with the help of this doc.
What i want to do here is,in my sampledata.xlsx file i have more data like flat,address,price,etc here i already don't know which fields are present in my excel sheet but i want all that to be console.log.How could i do this is there any way to do this.
import xlsx2json from 'xlsx2json';
OR
const xlsx2json = require('xlsx2json');
const excel2json = [
(req, res, next) => {
xlsx2json(req.body.file.path)
.then(result => result[0])
.reduce((object, item, index) => {
if (index === 0) {
object.mapper = item; // eslint-disable-line no-param-reassign
return object;
}
const data = {};
Object.keys(item).forEach((key) => {
data[object.mapper[key]] = item[key];
});
object.data.push(data);
return object;
}, { mapper: {}, data: [] })
.then(excel => console.log(excel)) // this gives json as output
.catch(err => next(err));
},
];
npm install xlsx-to-json-lc --save
npm install xls-to-json-lc --save
var exceltojson = require("xls-to-json-lc");
exceltojson({
input: "pass the input excel file here (.xls format)"
output: "if you want output to be stored in a file"
sheet: "sheetname", // specific sheetname inside excel file (if you have multiple sheets)
lowerCaseHeaders:true //to convert all excel headers to lowr case in json
}, function(err, result) {
if(err) {
console.error(err);
} else {
console.log(result);
//result will contain the overted json data
}
});