Using requireJS with svgPanZoom giving error: Uncaught TypeError: svgPanZoom is not a function - svgpanzoom

I have been using svg-pan-zoom library successfully with my javascript app but I now need to refactor it to use requireJS.
My util.js is:
define([
'baja!',
'jquery',
'/file/WebWidgets/js/libraries/svg-pan-zoom.js'
], function (
baja,
$,
svgPanZoom) {
'use strict';
const updateInitializeDiv = () => {
const svgDocument = $('#svgObjectElementFromBinding')[0].contentDocument;
const svgDocumentElement = svgDocument.documentElement;
console.log(svgDocumentElement);
console.log(svgDocumentElement.tagName);//svg
let panZoomSVG = svgPanZoom(svgDocumentElement, {
zoomEnabled: true,
controlIconsEnabled: true
});
}
const util = {};
util.updateInitializeDiv = updateInitializeDiv;
return util;
});
I am getting "Uncaught TypeError: svgPanZoom is not a function".
Can anyone suggest what I am doing wrong?

I had to reference the svg-pan-zoom library in the RequireJS config to get this to work.

Related

Can't wrap firebase function using firebase-function-testing

When I use the wrap function from firebase-functions-test I got this error message
Error Message
TypeError: Cannot read properties of undefined (reading 'length')
at isV2CloudFunction (node_modules/firebase-functions-test/lib/main.js:49:26)
at wrap (node_modules/firebase-functions-test/lib/main.js:32:9)
Test file code
const firebaseFunctionsTest = require("firebase-functions-test");
const {wrap} = firebaseFunctionsTest()
describe('Firestore Function Test', ()=>{
let wrappedFunction = wrap(firestoreFunction.firestoreFunction)
beforeEach(()=>{
wrappedFunction()
})
})
Function file code
exports.firestoreFunction = functions.firestore.document('/collection/doc').onCreate(
async(snap, context)=>{
//function logic
});
function functionA(){
//function A logic
}
function functionB(){
//function B logic
}
module.exports = {
functionA,
functionB
}
In the file function, I have used two methods of export,
so in the test file when I call firestoreFunction.firestoreFunction
it will be undefined.
Changing the function file has solved it.
const firestoreFunction = functions.firestore.document('/collection/doc').onCreate(
async(snap, context)=>{
//function logic
});
function functionA(){
//function A logic
}
function functionB(){
//function B logic
}
module.exports = {
firestoreFunction,
functionA,
functionB
}

node discord js export a function

I've been making a discord.js bot following the official guide.
I have all my commands in the /commands folder as advised.
Then I followed the course to create a currency system with sequelize, following this page from the same guide.
I have a balance.js file inside the commands folder, but when I'm calling it, it gives me this error:
TypeError: currency.getBalance is not a function
I've defined the function in my app.js file, but how can I export it (or use it) inside the balance.js which is called by the app.js file?
This is the function defined in the main file app.js:
Reflect.defineProperty(currency, 'getBalance', {
value: function getBalance(id) {
const user = currency.get(id);
return user ? user.balance : 0;
},
});
This is balance.js:
module.exports = {
name: 'balance',
description: 'Informs you about your balance.',
cooldown : 10,
guildOnly : true,
aliases: ['bal', 'cur', 'gem', 'gems'],
execute(message, args) {
const Discord = require('discord.js');
const { Users, CurrencyShop, UserItems, CardBase, UserCollec } = require('../dbObjects');
const currency = require('../app.js')
async () => { const storedBalances = await Users.findAll();
storedBalances.forEach(b => currency.set(b.user_id, b));
UserCollec.sync(); }
const target = message.author;
return message.channel.send(`${target} has ${currency.getBalance(target.id)}<:Gem:756059891465977886>`);
},
};
EDIT:
I progressed. Now I understand that I have to import the currency variable, which has been declared as a new Discord.Collection() in app.js.
I need to refer to this variable in a module and this module doesn't seem to see it as a collection. How do I import it?

Cannot read property 'isEmpty' of undefined when load multi model into Forge viewer

I following this example
https://forge.autodesk.com/blog/aggregate-multi-models-sequence-forge-viewer
for loading multi model into Forge Viewer but sometimes I got this error:
Cannot read property 'isEmpty' of undefined.
When i try to reload the page again sometime it don't have this error, i don't know why. Can you please help. Thank you very much
Snapshot
This is my request for token
function getForgeToken(callback) {
jQuery.ajax({
url: '/token',
success: function (res) {
callback(res.access_token, res.expires_in)
}
});
}
And for Initialize token error, Load failure error
var token = getForgeToken;
var options = {
env: 'AutodeskProduction',
getAccessToken: token
};
//It looks the static function of Viewer does not support ES6
//still use ES5
Autodesk.Viewing.Initializer(options, function onInitialized(){
//get the viewer div
const viewerDiv = document.getElementById( 'forgeViewerLocal' );
//initialize the viewer object
const viewer = new Autodesk.Viewing.Private.GuiViewer3D( viewerDiv);
//load model one by one in sequence
const util = new MultipleModelUtil( viewer );
util.processModels( models);
});
launchViewer( models.concat() );

Receiving the TypeError: r is not a function

I am receiving the following error highlighting callback(result);:
Uncaught TypeError: r is not a function at Image.onload (webp-support.js:6)
Exporting File (webp-support.js):
export default (callback) => {
let img = new Image();
img.onload = _ => {
let result = (img.width > 0) && (img.height > 0);
callback(result);
}
img.onerror = _ => {
callback(false);
}
img.src = "data:image/webp;base64,UklGRiIAAABXRUJQVlA4IBYAAAAwAQCdASoBAAEADsD+JaQAA3AAAAAA";
}
Importing File:
import webpSupport from './webp-support';
webpSupport(console.log('hello'));
The problem is in this line:
webpSupport(console.log('hello'));
If we break it down, you'll see the problem:
let supportedCallback = console.log('hello');
webpSupport(supportedCallback);
That obviously doesn't work. It does the console.log then assigns the result of that (undefined, always) as the callback in your webpSupport function. Clearly you don't want that. You need to pass a function that itself calls console.log:
webpSupport(() => console.log('webp supported'));
I've also taken the liberty of logging a more helpful message than hello! If you're not familiar with it, the () => ... syntax is Javascript's shorter function syntax, known as an arrow function.

Table to JSON using node.js

I am trying to convert a table to JSON, to search for data easily, the URL is: http://www.tppcrpg.net/rarity.html
I found this package:
https://www.npmjs.com/package/tabletojson
I tried to use it like:
'use strict';
const tabletojson = require('tabletojson');
tabletojson.convertUrl(
'http://www.tppcrpg.net/rarity.html',
{ useFirstRowForHeadings: true },
function(tablesAsJson) {
console.log(tablesAsJson[1]);
}
);
However it returns undefined in the console, are there any alternative options or am I using the package wrong?
Hey you are actually getting data, change the console.log
Your output have total one array only but you are putting tablesAsJson[1] in console, but array index starts with [0].
'use strict';
const tabletojson = require('tabletojson');
tabletojson.convertUrl(
'http://www.tppcrpg.net/rarity.html',
function(tablesAsJson) {
console.log(tablesAsJson[0]);
}
);
For better looking code:
const url = 'http://www.tppcrpg.net/rarity.html';
tabletojson.convertUrl(url)
.then((data) => {
console.log(data[0]);
})
.catch((err) => {
console.log('err', err);
}); // to catch error