I'm working on a project using meteor + react as front-and-back end.
For front-end UI, I am using element-react (https://eleme.github.io/element-react/#/en-US/quick-start) which is really cool and awesome. However when I tried to import element-react into my project (as instructed in the quick start of element-react homepage), meteor failed to compile static files and returned "Uncaught Error: Cannot find module './assets/error.svg''" which is the file do exist and has correct relative path.
Is there something missing or in meteor we simply can not use "require('./assets/error.svg')" to load a svg image?
According to this post in Meteor's forum.
You can use something like Meteor methods and the Assets API to get most any data from your server though. Something like
/server/main.js
Meteor.methods({
'svg.get'(data) {
return Assets.getText(data.path)
}
})
and
/client/main.js
const getSVG = async (path) => {
return await new Promise((resolve, reject) => {
Meteor.call('svg.get', { path }, (err, res) => {
if (err) reject('Something went wrong')
resolve(res)
})
})
}
const SVG = await getSVG('some/path/relative/to/private/file.svg')
Related
I have a commands folder containing files that contains slash commands, and i have to import the client instance from index.js to another file, but when doing :
module.exports = client;
it execute the entire index.js script ! I don't know what I'm doing wrong, I tried to make the instance in another file than index.js and then import the instance in index, but it doesn't work. Can someone help me?
At least from what I'm understanding, (assuming you already had registered the commands and had a client object set up) you could pass client through into command.execute() as an argument (on the 10th line in the snippet):
in index.js:
client.on(Events.InteractionCreate, async interaction => {
if (!interaction.isChatInputCommand()) return;
const command = interaction.client.commands.get(interaction.commandName);
if (!command) {
console.error(`No command matching ${interaction.commandName} was found.`);
return;
}
try {
console.log(`running command "${interaction.commandName}"!`);
await command.execute(interaction,client);
} catch (error) {
console.error(error);
await interaction.reply({ content: 'There was an error while executing this command!', ephemeral: true });
}
});
And then take that argument within the command's module:
In commands/yourcommand.js:
module.exports = {
//[...] other slash command setup bits and bobs
async execute(interaction, client) {
// your code here
}
}
modified version of discordjs guide snippets
Feel free to ask if there's any need for elaboration or clarification; hope this helps & cheers!
Most objects in discord.js have access to the client, using simply .client. Notice the Base class has a .client property, and all interactions extend BaseInteraction, which extends Base. This means using interaction.client will give you access to the client.
async execute(interaction) {
const client = interaction.client
}
I am trying to deploy this repo: https://github.com/DataStax-Examples/astra-tik-tok with Vercel instead of Netlify.
I refactored vanilla React into Next.js, but in the Home.js file I don't understand how to migrate this over to Vercel's equivalent:
//fetch all the tik-tok posts to your feed
const fetchData = async () => {
const results = await axios.get('/.netlify/functions/posts')
console.log(results.data)
setUsers(results.data)
}
Any ideas?
The equivalent in Next.js are API routes, which get deployed as serverless functions in Vercel. They must be created under the /pages/api folder.
You need to slightly refactor the functions to work with Next.js. For example, your add.js function would look like the following as an API route.
// /pages/api/add.js
export default function handler(req, res) {
const users = await getCollection();
try {
const user = await users.create(id, event.body);
res.status(200).json(user);
} catch (e) {
console.error(e);
res.status(500).json({ error: JSON.stringify(e) })
}
};
You would then call the API route from the client-side code by pointing to /api/add.
await axios.get('/api/add')
I am working on a Express/NodeJs project. I am new to Express/NodeJs, I am trying to import airportQuery.js into DistanceFormula.js. I am trying to directly import airportQuery.js from DistanceFormula.js. Im trying to call getAirports and return the answer to DistanceFormula.js. I not sure if I have to use the node routing or if i'm doing it correctly.
File Stucture:
File Structure
DistanceFormula.JS
import {getAirports} from "./api/airportQuery";
console.log(getAirports('3c675a'));
AirportQuery.js
async function getAirports(planeIcao) {
let airport = {
arrival: "",
destination: ""
};
const airport_url = 'https://opensky-network.org/api/flights/aircraft?icao24=' + planeIcao + '&begin=1517184000&end=1517270400';
const response = await fetch(airport_url);
const data = await response.json();
console.log(data[0]);
console.log(data[0].estArrivalAirport);
airport.arrival = data[0].estArrivalAirport;
console.log(data[0].estDepartureAirport);
airport.destination = data[0].estDepartureAirport;
return airport
}
const fetch = require("node-fetch");
export {getAirports};
ERROR: Uncaught SyntaxError: Cannot use import statement outside a module
To use modules in node.js, you have to do the following:
Be running a version of nodejs that supports ESM modules (v8.5+).
Run with this command line flag: node --experimental-modules
Name your file with an .mjs file extension OR specify it as a module in package.json
See the relevant documentation for more info.
This is true not only for the top level file you import, but if it also uses import, then the same rules above have to apply to it too.
Note, that once you get your modules to load properly, you will then have a problem with this line of code because getAirports() returns promise, not a value. All async functions return a promise, always. The return value in the function will become the resolved value of the returned promise. That's how async functions work. So, change this:
console.log(getAirports('3c675a'));
To this:
getAirports('3c675a').then(result=> {
console.log(result);
}).catch(err => {
console.log(err);
});
I'm making the Meteor.js app and I have to get data from mssql database. I use Meteor 1.8 and a npm package - mssql(version 5.1.0). That's my code:
const sql = require('mssql')
const config = {something}
export default function fetch(query) {
const config = {something}
sql.connect(config, err => {
if (err) {
console.error(err)
return
}
// create Request object
var request = new sql.Request()
// query to the database and get the records
request.query(query, (err, recordset) => {
if (err) {
console.error(err)
return
}
return recordset
})
})
}
And I have such error
TypeError: sql.connect is not a function
I don't know what's going on, I tried to do it in many ways and I failed. When I use ConnectionPool I see an error saying that ConnectionPool is not a constructor.
What is the proper way to do this?
In my config file I have: user, password, server, database, port.
It appears to be because you're mixing your module systems, you're using require (which is CommonJS-like) but in something that's apparently an ECMAScript module (ESM) (from the export default ...). Node.js itself won't let you do that, but if you're using some kind of bundler or transpiler, perhaps it might...
Changing your require to:
import sql from "mssql";
...and making sure Node.js is using ESM¹, I don't get any error with sql.connect not being a function.
¹ In my case, since I'm using Node.js v12, I do that by having "type": "module" in my package.json and running it via node --experimental-modules filename.js, more here.
I am trying to fetch all the instance types for EC2 from EC2 SDK JSON
const instanceEnums = require('aws-sdk/apis/ec2-2016-11-15.normal.json');
function getAllTypes(property) {
return instanceEnums.shapes[property].enum;
}
getAllTypes('InstanceType')
But It throws an error that
cannot find module aws-sdk/apis/ec2-2016-11-15.normal.json
I realized that the installed SDK/module does not include the .normal.json file but only .min.js file.
Is there any other way to access the files from apis folder same as we can access clients folder just by requiring SDK and AWS.EC2 and all(as sdk exports the clients folder's files from index.js.)
I need to use something like explained as in this answer https://stackoverflow.com/a/42494509/9381809
You can download ec2-2016-11-15.normal.json directly from github (on the application startup for example) and use it like as follows:
const axios = require('axios');
const url = 'https://raw.githubusercontent.com/aws/aws-sdk-js/master/apis/ec2-2016-11-15.normal.json';
const getAllTypes = (() => {
const loadApi = axios.get(url);
return (property) => loadApi.then(response => {
return response.data.shapes[property].enum
})
})();
getAllTypes('InstanceType').then((types) => {
console.log(types);
});
The code below works for me.
const instanceEnums = require('aws-sdk/apis/ec2-2016-11-15.min.json');
console.log(instanceEnums.shapes);