How to set a model to a variable in loopback? - node.js

fallowing code returns list of models
var models = app.models();
models.forEach(function(Model) {
console.log(Model.modelName);
});
User
AccessToken
ACL
RoleMapping
Role
Registration
Assets
when I using fallowing code to use Registration or Asstes:
D:\apps\newapps\testapp\node_modules\loopback\lib\application.js:129
assert(Model.prototype instanceof Model.registry.getModel('Model'),
^
TypeError: Cannot read property 'prototype' of undefined
at Function.app.model (D:\apps\newapps\testapp\node_modules\loopback\lib\application.js:129:17)
at Object.<anonymous> (D:\apps\newapps\testapp\server\server.js:38:5)
at Module._compile (module.js:573:30)
at Object.Module._extensions..js (module.js:584:10)
at Module.load (module.js:507:32)
at tryModuleLoad (module.js:470:12)
at Function.Module._load (module.js:462:3)
at Function.Module.runMain (module.js:609:10)
at startup (bootstrap_node.js:158:16)
at bootstrap_node.js:598:3

I am not expert of loopback but try this code:
var Registration = app.models.Registration;
console.log(Registration);
app.model(Registration);

User is the model name.
var user = app.models.User;
resetPassword is function name of the user model, which
take two parameter new_pwd and token,
user.resetPassword({
new_pwd: new_password,
token
}, (err, data) => {
if (err || !data.status) {
return err || data;
} else {
return data;
}
});

Related

importing mongodb results in error that is not related to mongodb when running the script using node command

I have a stand alone script that I am using to create an admin user. There will always be only one admin user. However, when I run the standalone script using node command in my nextjs project, I get this weird error below
node indexUser.js
/Users/nabeel.77.dev/Documents/portfolio/node_modules/whatwg-url/lib/utils.js:50
var AsyncIteratorPrototype = Object.getPrototypeOf(Object.getPrototypeOf( /*#__PURE__*/_wrapAsyncGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee() {
^
TypeError: Cannot convert undefined or null to object
at Function.getPrototypeOf (<anonymous>)
at Object.<anonymous> (/Users/nabeel.77.dev/Documents/portfolio/node_modules/whatwg-url/lib/utils.js:34:39)
at Module._compile (node:internal/modules/cjs/loader:1218:14)
at Module._compile (/Users/nabeel.77.dev/Documents/portfolio/node_modules/pirates/lib/index.js:136:24)
at Module._extensions..js (node:internal/modules/cjs/loader:1272:10)
at Object.newLoader [as .js] (/Users/nabeel.77.dev/Documents/portfolio/node_modules/pirates/lib/index.js:141:7)
at Module.load (node:internal/modules/cjs/loader:1081:32)
at Function.Module._load (node:internal/modules/cjs/loader:922:12)
at Module.require (node:internal/modules/cjs/loader:1105:19)
at require (node:internal/modules/cjs/helpers:103:18)
at Object.<anonymous> (/Users/nabeel.77.dev/Documents/portfolio/node_modules/whatwg-url/lib/URL.js:4:15)
at Module._compile (node:internal/modules/cjs/loader:1218:14)
at Module._compile (/Users/nabeel.77.dev/Documents/portfolio/node_modules/pirates/lib/index.js:136:24)
at Module._extensions..js (node:internal/modules/cjs/loader:1272:10)
at Object.newLoader [as .js] (/Users/nabeel.77.dev/Documents/portfolio/node_modules/pirates/lib/index.js:141:7)
at Module.load (node:internal/modules/cjs/loader:1081:32)
at Function.Module._load (node:internal/modules/cjs/loader:922:12)
at Module.require (node:internal/modules/cjs/loader:1105:19)
at require (node:internal/modules/cjs/helpers:103:18)
at Object.<anonymous> (/Users/nabeel.77.dev/Documents/portfolio/node_modules/whatwg-url/webidl2js-wrapper.js:3:13)
at Module._compile (node:internal/modules/cjs/loader:1218:14)
at Module._compile (/Users/nabeel.77.dev/Documents/portfolio/node_modules/pirates/lib/index.js:136:24)
at Module._extensions..js (node:internal/modules/cjs/loader:1272:10)
at Object.newLoader [as .js] (/Users/nabeel.77.dev/Documents/portfolio/node_modules/pirates/lib/index.js:141:7)
at Module.load (node:internal/modules/cjs/loader:1081:32)
at Function.Module._load (node:internal/modules/cjs/loader:922:12)
at Module.require (node:internal/modules/cjs/loader:1105:19)
at require (node:internal/modules/cjs/helpers:103:18)
at Object.<anonymous> (/Users/nabeel.77.dev/Documents/portfolio/node_modules/whatwg-url/index.js:3:34)
at Module._compile (node:internal/modules/cjs/loader:1218:14)
at Module._compile (/Users/nabeel.77.dev/Documents/portfolio/node_modules/pirates/lib/index.js:136:24)
at Module._extensions..js (node:internal/modules/cjs/loader:1272:10)
at Object.newLoader [as .js] (/Users/nabeel.77.dev/Documents/portfolio/node_modules/pirates/lib/index.js:141:7)
at Module.load (node:internal/modules/cjs/loader:1081:32)
at Function.Module._load (node:internal/modules/cjs/loader:922:12)
at Module.require (node:internal/modules/cjs/loader:1105:19)
at require (node:internal/modules/cjs/helpers:103:18)
at Object.<anonymous> (../src/index.ts:1:1)
at Module._compile (node:internal/modules/cjs/loader:1218:14)
at Module._compile (/Users/nabeel.77.dev/Documents/portfolio/node_modules/pirates/lib/index.js:136:24)
at Module._extensions..js (node:internal/modules/cjs/loader:1272:10)
at Object.newLoader [as .js] (/Users/nabeel.77.dev/Documents/portfolio/node_modules/pirates/lib/index.js:141:7)
at Module.load (node:internal/modules/cjs/loader:1081:32)
at Function.Module._load (node:internal/modules/cjs/loader:922:12)
at Module.require (node:internal/modules/cjs/loader:1105:19)
at require (node:internal/modules/cjs/helpers:103:18)
at Object.<anonymous> (../src/connection_string.ts:3:1)
at Module._compile (node:internal/modules/cjs/loader:1218:14)
at Module._compile (/Users/nabeel.77.dev/Documents/portfolio/node_modules/pirates/lib/index.js:136:24)
at Module._extensions..js (node:internal/modules/cjs/loader:1272:10)
Node.js v19.3.0
The script I am running has the below code:
import bcrypt from 'bcryptjs';
import MongoClientConnection from '../db';
import readline from 'readline';
import { logger } from '../logger';
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
rl.question('Enter username: ', (username) => {
rl.question('Enter email: ', (email) => {
rl.question('Enter password: ', async (password) => {
const hashedPassword = await bcrypt.hash(password, 10);
const adminUserData = { username, email, hashedPassword };
try {
const db = await MongoClientConnection.Get();
const res = await db.insert(adminUserData);
} catch (e) {
logger.error(e);
}
rl.close();
});
});
});
In the code above, when I remove import MongoClientConnection from '../db'; the script works fine so, I believe the error is because of mongodb. I am using singleton connection for mongodb and have the below code for that
import { MongoClient } from 'mongodb';
const MongoClientConnection = (() => {
let db = null;
let instance = 0;
const DbConnect = async () => {
try {
let url = process.env.DB_URL;
let mongoClient = await MongoClient.connect(url, {
useUnifiedTopology: true,
});
const mongoDbInstance = mongoClient.db(process.env.DB);
return mongoDbInstance;
} catch (e) {
return e;
}
};
const Get = async () => {
try {
instance++; // this is just to count how many times our singleton is called.
console.log(`DbConnection called ${instance} times`);
if (db != null) {
console.log(`db connection is already alive`);
return db;
} else {
console.log(`getting new db connection`);
db = await DbConnect();
return db;
}
} catch (e) {
return e;
}
};
return {
Get: Get,
};
})();
export default MongoClientConnection;
Cannot really find any solution to this.

TypeError: Cannot read properties of undefined (reading 'getBalance') in node.js

I am getting error when using web3.js to get the balance of an account. I am using ganache. My code is below,
var Web3 = require("web3");
//connect with ganache
const ganacheWeb3 = new Web3(
new Web3.providers.HttpProvider("HTTP://127.0.0.1:7545")
);
console.log(ganacheWeb3);
//check the balance of an account
const balanceOfAccount = Web3.Eth.getBalance(
"0xaEA4e665291fdBFe4bAFc5b81F6F213551180ab5"
);
console.log(
balanceOfAccount.then((result) =>
console.log(Web3.utils.fromWei(result, "ether"))
)
);
Web3.eth.getBalance(
"0xaEA4e665291fdBFe4bAFc5b81F6F213551180ab5",
(error, result) => {
if (error) {
console.log(error);
} else {
console.log(result);
}
}
);
I have used the normal functional way and callback way. I don't know which one is correct. But still I am getting error. The error is,
const balanceOfAccount = Web3.Eth.getBalance(
^
TypeError: Cannot read properties of undefined (reading 'getBalance')
at Object.<anonymous> (D:\Blockchain Development\Web3.js\intro-to-web3.js\index.js:10:35)
at Module._compile (node:internal/modules/cjs/loader:1159:14)
at Module._extensions..js (node:internal/modules/cjs/loader:1213:10)
at Module.load (node:internal/modules/cjs/loader:1037:32)
at Module._load (node:internal/modules/cjs/loader:878:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:82:12)
at node:internal/main/run_main_module:23:47
I am trying to get the value of an ethereum account. But getting an error.
Change Web3.Eth.getBalance(...) to Web3.eth.getBalance(...).
Documentation: https://web3js.readthedocs.io/en/v1.2.11/web3-eth.html#getbalance

TypeError: Cannot read property 'showMessageBox' of undefined electron in node js

Below code is just to display the dialog box using electron node module.
app.js
const { dialog } = require('electron')
const response = dialog.showMessageBox(null);
console.log(response);
Need help to understand why I am getting below error message:
const response = dialog.showMessageBox(null);
^
TypeError: Cannot read property 'showMessageBox' of undefined
at Object.<anonymous> (C:\Users\1217688\Desktop\WebApp\node-elect-test\app.js:2:25)
at Module._compile (module.js:660:30)
at Object.Module._extensions..js (module.js:671:10)
at Module.load (module.js:573:32)
at tryModuleLoad (module.js:513:12)
at Function.Module._load (module.js:505:3)
at Function.Module.runMain (module.js:701:10)
at startup (bootstrap_node.js:190:16)
at bootstrap_node.js:662:3
In renderer process, add .remote
const { dialog } = require('electron').remote
Try calling the dialog module when the app is ready:
app.on('ready', () => {
const { dialog } = require('electron')
dialog.showMessageBox(null);
})

RestClient has been removed from this version of the library (TWILIO)

throw new Error(this.constructor.name + ' has been removed from this
version of the library. Please refer to
https://www.twilio.com/docs/libraries/node for more information.')
^ Error: RestClient has been removed from this version of the library. Please refer to https://www.twilio.com/docs/libraries/node
for more information.
at RestClient.ObsoleteClient (C:\carsapp\node_modules\twilio\lib\base\obsolete.js:7:9)
at new RestClient (C:\carsapp\node_modules\twilio\lib\base\obsolete.js:12:21)
at Object. (C:\carsapp\helpers\twilio_restful_client_connection.ts:6:34)
at Module._compile (module.js:643:30)
at Module.m._compile (C:\Users\Andrey Radkevich\AppData\Roaming\npm\node_modules\ts-node\src\index.ts:422:23)
at Module._extensions..js (module.js:654:10)
at Object.require.extensions.(anonymous function) [as .ts] (C:\Users\Andrey
Radkevich\AppData\Roaming\npm\node_modules\ts-node\src\index.ts:425:12)
at Module.load (module.js:556:32)
at tryModuleLoad (module.js:499:12)
at Function.Module._load (module.js:491:3)
at Module.require (module.js:587:17)
at require (internal/module.js:11:18)
at Object. (C:\carsapp\helpers\sing-in-up-helper.ts:8:1)
at Module._compile (module.js:643:30)
at Module.m._compile (C:\Users\Andrey Radkevich\AppData\Roaming\npm\node_modules\ts-node\src\index.ts:422:23)
at Module._extensions..js (module.js:654:10)
const accountSid = 'AC2a54bb2c4ea4992593cc9f0ca2f720c0';
const authToken = 'your_auth_token';
const Client = require('twilio').RestClient;
const client = new Client(accountSid, authToken);
client.outgoingCallerIds.create(
{
friendlyName: 'My Home Phone Number',
phoneNumber: '+14158675310',
},
(err, callerId) => {
if (err) {
console.error(err);
} else {
console.log(callerId.sid);
}
}
);
I have got this error when I tried to create this part of the code above. I tried to add a number to Twilio whitelist ( verify phone number).How can I make it in another way? I use for this Node js
Link on this part of the code in the documentation :
https://www.twilio.com/docs/api/voice/outgoing-caller-ids
const accountSid = 'AC2a54bb2c4ea4992593cc9f0ca2f720c0';
const authToken = 'your_auth_token';
const client = require('twilio')(accountSid, authToken);
client.validationRequests
.create({
friendlyName: 'My Home Phone Number',
phoneNumber: '+14158675310',
})
.then(data => console.log(data.validationCode));
https://www.twilio.com/docs/api/voice/outgoing-caller-ids#add-an-outgoing-caller-id
Here answer on my own question

I am creating objecting in the classes of a instance at "Syncano' . I am reveiving this ReferenceError: callback is not defined

Here's the error log
ReferenceError: callback is not defined
at Object. (C:\Users\Username\Desktop\JsGame-master\APP.JS:137:45)
at Module._compile (module.js:413:34)
at Object.Module._extensions..js (module.js:422:10)
at Module.load (module.js:357:32)
at Function.Module._load (module.js:314:12)
at Function.Module.runMain (module.js:447:10)
at startup (node.js:146:18)
at node.js:404:3
var connection = Syncano({apiKey: 'abc',
userKey: 'abc',
defaults: {
instanceName: "interactiveboard",
className: "players"
}
});
var DataObject = connection.DataObject;
DataObject .please() .list() .then(function(res) {
console.log(res);
});
var dataObject = {
avatar: "Geralt",
nickname: "Rivia",
email:"whatevershit#gmail.com"
};
DataObject.please().create(DataObject).then(callback);
This is happening because the callback you are passing into DataObject.please().create(DataObject).then(callback); is not defined anywhere.
You could solve this in two ways.
One would be to define a callback before you pass it into that call like this:
var callback = function(res) { console.log(res); };
The other would be to change your last line to pass the function directly into the then call like this:
DataObject.please().create(DataObject).then(function(res) {
console.log(res);
});
Hope this helps!

Resources