App Object in Electron Module and getAppPath throws Error - node.js

I have a strange problem with my application. I get an error, and I can't solve it. First at all, I installed a new project, so everything is clean. Someone sent me this repo to use for an Angular, Electron and Nodejs Application. Everything worked fine, but then I decided to install an embedded database like sqlite3. For this I found NeDB, and the module is perfect for my needs. First I had the problem, has nothing to do with my general problem, that I can't create a database file. So I read that I can only create files in my application path, because something about Electron and that's working in a browser.
I found the getAppPath() method that is implemented in my app object from the Electron module. Here starts the problem. For hours I tried to get the application path from this object. Finally, I wrote this code.
import { Injectable } from '#angular/core';
var nedb = require('nedb');
import { app } from 'electron';
import * as path from 'path';
#Injectable()
export class DatabaseService {
app: typeof app;
Database: any;
constructor() {
this.app = window.require("electron").app;
this.Database = new nedb({ filename: path.join(this.app.getAppPath(), '/diary.db'), autoload: true, timestampData: true });
var scott = {
name: 'Scott',
twitter: '#ScottWRobinson'
};
this.Database.insert(scott, function(err, doc) {
console.log('Inserted', doc.name, 'with ID', doc._id);
});
}
}
And I get this error.
I found this posting, but I don't really understand what the post is trying to tell me. I followed the links, but nothing seems to help. Anyone have an idea?

Related

Adminjs ComponentLoader not found

I have been trying to make custom component in Adminjs 6.6.5 dashboard but Adminjs ComponentLoader not found error occurs. then i have tried to
import AdminJS from 'adminjs'
const {ComponentLoader} = AdminJS
but i get: Trying to bundle file 'file:/Users/Josip/WebstormProjects/ferry-backend/components/dashboard.jsx' but it doesn't exist
I would really appreciate help...
admin/index.js
import {ComponentLoader} from "adminjs";
const componentLoader = new ComponentLoader()
const Components = {
MyDashboard: componentLoader.override('Dashboard','../components/dashboard.jsx')
}
export { componentLoader, Components }
index.js
import {componentLoader, Components} from "./admin/index.js";
AdminJS.registerAdapter(AdminJSSequelize)
const admin = new AdminJS({
databases: [],
rootPath: '/admin',
resources:[UsersResources, GuestResources, SalesResources, FinancesResources],
components:{
edit: Components.MyDashboard
},
componentLoader
})
you are fixed this problem? I also ran into this problem and don't understand how to solve it
i fixed this problem. You need to do the import as follows
import AdminJs from 'adminjs';
// And now you can use any adminjs dependencies as follows
AdminJs.ValidationError(errors)
My usage example:
if(Object.keys(errors).length) {throw new AdminJs.ValidationError(errors)} else {...}
Good luck with your development!

How to use mysql2 library with Sapper?

I am creating an application in Svelte Sapper. I have a routes/account/login.js API route where I am trying to use mysql2. The route itself works (I checked with Postman), but as soon as I import mysql, the server crashes and an error appears:
[rollup-plugin-svelte] The following packages did not export their `package.json` file so we could not check the "svelte" field. If you had difficulties importing svelte components from a package, then please contact the author and ask them to export the package.json file.
- mysql2
import mysql from "mysql2/promise";
export async function post(req, res) {
//route test
const { login, password } = req.body;
res.end(`${login}, ${password}`);
}
What can I do to make this import work?
What can I do to make this import work?
The Sapper documentation doesn't say anything about whether you need to change something in the configuration additionally. https://sapper.svelte.dev/docs#Server_routes
I found a solution. I had to create a #lib folder in the src/node_modules folder and there file eg. db.js. In that file instead of import you need to use require()! and then you have to export the function that connects to the database. and then you can import that in the path
//src/node_modules/#lib/db.js
const mysql = require("mysql2");
export async function connectToDatabase() {
return mysql.createConnection({
host: "localhost",
....
})
}
//routes/account/login.js
import { query } from "#lib/db";
export async function post(req, res) {
...
}

Problem using a npm package in React - says "TypeError: require is not a function"

I'm trying to use the package lastfm within a React app.
I have installed it with
npm install lastfm
The official doc says it should be used like this :
var LastFmNode = require('lastfm').LastFmNode;
var lastfm = new LastFmNode({
api_key: 'abc',
secret: 'secret'
});
And this is how I use it in my React code :
import {LASTFM_API,LASTFM_API_SECRET} from "./Constants";
import {LastFmNode} from 'lastfm';
export class Lastfm {
static scrobbleTrack(track): void {
var lastfm = new LastFmNode({
api_key: LASTFM_API,
secret: LASTFM_API_SECRET,
useragent: 'appname/vX.X MyApp'
});
}
}
Which fires this error (from the package) :
TypeError: require is not a function at lastfm-request.js:3
I'm quite new to React and NPM.
I wonder if there is some kind of incompatibility ?
How could I make this work ?
Thanks !
The package is to be used on Node server and not on React App.
EDIT
Hi, sorry i cannot comment because my points are less.
You can not use that one as well because that is also a node package. If you see a syntax where you have to use: require('something'); usually means it is a node package because in React, your syntax for importing is something like this: const abc from 'xyz';

Storing GET from my Node API to my Angular service

I'm sorry if this has already been asked, I've been through tons of posts and docs, but nothing quite sounds like what I'm trying to do, even if I think that it's rather simple.
I'm building a tiny web app. It has 2 directories : api (node, working on :8080) and app (angular, working on :4200). They work pretty well separately, my database is ready, everything is fine. Thanks to Json Proxy (I don't use CORS), my API routes are working in my Angular app, for instance :
localhost:4200/api/users URL will GET all my users, no problem. I thought I've done the hardest part, but now I'm stuck on one simple thing : How the hell do I use these API routes in TypeScript for my Angular Service. Like, from this service, I want to manipulate the datas already accessible from localhost:4200/api/users. Nothing more. How to use/call this URL/route in code ?
I'm confused by the lack of doc on this matter, sounds like such a basic thing to me. If anyone can point me to the right direction, I'd be glad. Thanks a lot !
Angular and nodejs routes are different. localhost:4200/api/users
//services.service.ts
callapifunction(name: string, pass: string, confirmPass: string): Observable<any> {
const body = new HttpParams()
.set('username', name)
.set('password', pass)
return this.http.post(
`localhsot:8080/api/users`,
body
)
.pipe(
tap(_ => console.log(_)),
catchError(this.handleError<any>('verify'))
);
}
You can call it any component. Thats it. Angular routers declared in router.
//app-routing.module.ts
import {Route} from '#angular/router';
import { HomeComponent } from './home/home.component';
export const routerConfig: Route[] = [
{
path: '',
children:[
//admin
{
path: 'api/users',
component: DashboardComponent
},
{
path: 'main/login',
component: LoginComponent
},
]},
{
path: '**',
redirectTo: '/'
}
];

How to use node.js module with Angular 6?

I am newbie in Angular 6 and TypeScript. Can someone show me the right way to use node.js third-party module in Angular 6?
For example i want to create component with ability to consume and make requests to SOAP wsdl methods.
Did installed it by adding to package.json with npm install.
Trying to use node-soap npm module like this:
import { Injectable } from '#angular/core';
import * as soap from 'node-soap';
#Injectable({
providedIn: 'root'
})
export class MySoapService {
constructor() { }
getOrderInfo() {
const url = 'http://my-example-api.com/WCF/ClientService.svc?singleWsdl';
let args = {
login: 'login',
password: 'password',
orderNumber: 'F976638'
};
soap.createClient(url, function(err, client) {
client.GetOrderInfo(args, function(err, result) {
console.log(result);
});
});
};
}
Then inject this service into component and render it, just to test the service...
But got some error during ng serve:
ERROR in ./node_modules/node-soap/client.js
Module not found: Error: Can't resolve 'http' in '/home/cadistortion/WebstormProjects/my-ng-app/node_modules/node-soap'
Thank you!
it's easy, in your url have your API node.
You go need more, express.js(or other) for get work.
BD - like MongoDB or other or Json.
See this vĂ­deo and other the same user https://www.youtube.com/watch?v=M-G48Gf2Xl0
is complete how create blog using MEAN Stack

Resources