How can I transform this require in a ES6 import statement? - import

var epayco = require('epayco-sdk-node')({
apiKey: 'PUBLIC_KEY',
privateKey: 'PRIVATE_KEY',
lang: 'ES',
test: true
})
I do not have any idea, totally lost in this. Also seems like there is another error when I try
import epayco from "epayco-sdk-node"
a message when the mouse is over appear: "Could not find a declaration file for module".

Related

importing schema from file causes ReferenceError: Cannot access 'MySchema' before initialization

I have a schema that I export from a file
// Vendor.ts
export { ShortVendorSchema };
const ShortVendorSchema = new Schema<TShortVendor>({
defaultVendor: Boolean,
companyName: String,
vendorId: { type: Schema.Types.ObjectId, ref: 'Vendor' },
});
and import it to another schema
// Order.ts
import { ShortVendorSchema } from './Vendor';
const OrderSchema = new Schema<TOrderSchema>(
{
status: {
type: String,
enum: Object.keys(ORDER_STATUS),
default: 'NEW',
},
vendor: ShortVendorSchema,
},
{ timestamps: true }
);
Whenever I'm trying to access Order model I get error error - ReferenceError: Cannot access 'ShortVendorSchema' before initialization
I have an idea why it happens, something about api routes living in isolation, but I don't know how to solve it without duplicating ShortVendorSchema in all files where it's used.
upd: moved all model exports into a single file, now I'm getting error error - TypeError: Cannot read properties of undefined (reading 'ShortVendorSchema')
Any ideas?
The error occurs because of circular dependency.
I was importing ShortVendorSchema to Order.ts file and OrderSchema to Vendor.ts file.
The solution is to move ShortVendorSchema to a separate file that doesn't have any imports.

SvelteKit console error "window is not defined" when i import library

I would like to import apexChart library which using "window" property, and i get error in console.
[vite] Error when evaluating SSR module /src/routes/prehled.svelte:
ReferenceError: window is not defined
I tried use a apexCharts after mount, but the error did not disappear.
<script>
import ApexCharts from 'apexcharts'
import { onMount } from 'svelte'
const myOptions = {...myOptions}
onMount(() => {
const chart = new ApexCharts(document.querySelector('[data-chart="profit"]'), myOptions)
chart.render()
})
</script>
I tried import a apexCharts when i am sure that browser exist.
import { browser } from '$app/env'
if (browser) {
import ApexCharts from 'apexcharts'
}
But i got error "'import' and 'export' may only appear at the top level"
I tried disable ssr in svelte.config.js
import adapter from '#sveltejs/adapter-static';
const config = {
kit: {
adapter: adapter(),
prerender: {
enabled: false
},
ssr: false,
}
I tried to create a component in which I import apexChart library and I created a condition that uses this component only if a browser exists
{ #if browser }
<ProfitChart />
{ /if }
Nothing helped.
Does anyone know how to help me please?
The easiest way is to simply include apexcharts like a standalone library in your webpage like this:
<script src="https://cdn.jsdelivr.net/npm/apexcharts"></script>
And then simply use it in the onMount:
onMount(() => {
const chart = new ApexCharts(container, options)
chart.render()
})
You can add this line either in your app.html or include it where it's required with a <svelte:head> block.
An alternative way would be to dynamically import during onMount:
onMount(async () => {
const ApexCharts = (await import('apexcharts')).default
const chart = new ApexCharts(container, options)
chart.render()
})
As an extra: use bind:this instead of document.querySelector to get DOM elements, that would be the more 'svelte' way.
I have found the last option with the Vite plugin to work best with less code in the end but will lose intellisense in vscode and see import highlighted as error (temp workaround at end): https://kit.svelte.dev/faq#how-do-i-use-x-with-sveltekit-how-do-i-use-a-client-side-only-library-that-depends-on-document-or-window
Install vite plugin: npm i -D vite-plugin-iso-import
Add plugin to svelte.config.js:
kit: {
vite: {
plugins: [
isoImport(),
],
Add plugin to TypeScript config (if you use TS):
"compilerOptions": {
"plugins": [{ "name": "vite-plugin-iso-import" }],
Use as normal but note the "?client" on the import:
<script context="module">
import { chart } from 'svelte-apexcharts?client';
import { onMount } from 'svelte'
let myOptions = {...myOptions}
onMount(() => {
myOptions = {...updated options/data}
});
</script>
<div use:chart={myOptions} />
Debugging note:
To have import not highlighting as an error temporarily, just:
npm run dev, your project will compile fine, then test in browser to execute at least once.
remove ?client now, save and continue debugging as usual.
For all of you trying to import dynamically into a js or ts file, try the following:
Import your package during on mount in any svelte component.
onMount(async () => {
const Example = await import('#creator/examplePackage');
usePackageInJSOrTS(Example.default);
});
Use the imported package in your js/ts function. You need to pass the default value of the constructor.
export function usePackageInJsOrTs(NeededPackage) {
let neededPacakge = new NeededPackage();
}

Can't import tween using import

I have an es6 script that include a tween library with es6 import. The code works fine if it's not transpiled, I mean I can import tween and use it inside the script, if I use webpacke with my configuration the script exit with an error becouse the TWEEN is somehow undefined.
I have tried change extension of tween file with mjs but it generate other errors like
require is not defined
I webpack add core-js modules using require funcition
require("core-js/modules/es.symbol");
The code with issue
'use strict';
...
import {TWEEN} from '../threejs/tween.js';
//import {TWEEN} from '../threejs/tween.js';
...
export class CustomClass extends ParentClass {
constructor(arguments) {
super(arguments);
this.tweenGroup = new TWEEN.Group(); // the line that generate "Cant get Group of undefined"
}
...
}
...
this is my babel-loader configuration configuration
{
test: /\.m?js$/,
exclude: /(node_modules|bower_components)/,
use: {
loader: 'babel-loader',
options: {
sourceType: 'module'
}
}
},
my babel configuration is look like this
module.exports = {
presets: [
[
'#babel/preset-env',
{
debug: true,
useBuiltIns: 'usage',
corejs: 3
}
]
],
plugins: [
// '#babel/plugin-proposal-class-properties',
// '#babel/plugin-transform-runtime',
// '#babel/runtime-corejs2',
// '#babel/plugin-syntax-dynamic-import',
// '#babel/plugin-syntax-async-generators',
// '#babel/plugin-transform-regenerator',
// '#babel/plugin-transform-async-to-generator',
'#babel/plugin-transform-modules-commonjs',
// '#babel/plugin-transform-typeof-symbol'
]
};
The code with TWEEN import (the first block of code I posted) work just fine if I use it in a project that is not transpiled with babel, but it generate the error if I run devserver. I don't understand why I can't import the Tween properly afetr compilation.
I will apriciate any help.l
Update: I realized I had rather different setup and different issue than you did, but while searching the solution I found this thread with recommendation for npm intalling tween for babel workflow. Maybe that is helpful for you.
I still leave my initial reply here, could be useful for anyone else:
I had some trouble including TWEEN within my js project (eg using import * as TWEEN from '...'), but following the those steps for simple installation I got it working after I included it in HTML, not in js:
<script src="js/libs/Tween.js"></script>

MEAN - Node - Mongoose - Error

trying create simple API using MEAN.
am following below youtube link as guide
https://www.youtube.com/watch?v=MMOIr_VwwAk
setting port on 3000. when try to run it, throws error like as fallows
**mongoose.connect('mongodb://localhost/restful');
^
TypeError: Cannot read property 'connect' of undefined**
server.js
var express = require ('express'),
restful = require('node-rest-client'),
mongoose = restful.mongoose;
var app = express();
app.get(function(){
app.use(express.bodyParser());
app.use(express.methodOverride());
});
mongoose.connect('mongodb://localhost/restful');
var ProductSchema = mongoose.Schema({
name : String,
sku: String,
price:Number
});
var Products =restful.model('products',ProductSchema);
Products.methods(['get','put','post','delete']);
Products.register(app,'/api/products');
app.listen(3000);
console.log("am running on port 3000");
package.json
{
"name": "restful",
"main": "server.js",
"dependencies": {
"express": "^4.14.0",
"mangoose": "latest",
"node-restful": "latest"
}
}
this what have start with as of now.
helps much appreciated.
Your package.json appears to have typos. Did you get errors when you ran npm install?
I assume mangoose is meant to be mongoose.
Also node-restful does not match any of your require statements. Instead you have restful = require('node-rest-client')

Cannot save a document in Mongoose - Validator required failed for path error

Following is my schema:
var userSchema = new Schema({
username: {
type: String,
required: true
},
password: {
type: String,
required: false
}
});
Now, when I attempt to save a document of the above schema, I get the following error:
{ message: 'Validation failed',
name: 'ValidationError',
errors:
{ username:
{ message: 'Validator "required" failed for path username',
name: 'ValidatorError',
path: 'username',
type: 'required' } } }
The above is the error object returned by mongoose upon save. I searched for this error but could not understand what is wrong. The document that I am trying to save is as follows:
{
username: "foo"
password: "bar"
}
Any idea what this means? I searched the mongoose docs too but could not find anything under the validation section.
First, you are missing a comma (,) after foo.
Now, is { username: "foo", password: "bar" } JSON sent via http, our an actual object in your server-side code ?
If it is, try to console.log(youVariable.username) and see if it shows undefined or the value foo. If you see undefined, then your object is not parsed properly.
You can make sure that whom ever is sending the POST request is sending a "application/json" in the header, you could be receiving something else, thus your JSON isn't parsed to a valid javascript object.