Error on App.contracts.TodoList = TruffleContract(todoList) - truffle

Working on Windows 10 Environment
Error from web server on windows 10:
'''
Failed to load resource: the server responded with a status of 404 (Not Found)
app.js:56 Uncaught (in promise) ReferenceError: TruffleContract is not defined
at Object.loadContract (app.js:56)
at async Object.load (app.js:8)
'''
Code referenced in error:
'''
loadContract: async () => {
//var contract = require("truffle-contract");
// Create a JavaScript version of the smart contract
const todoList = await $.getJSON('vote.json')
console.log(todoList)
App.contracts.TodoList = TruffleContract(todoList)
App.contracts.TodoList.setProvider(App.web3Provider)
// Hydrate the smart contract with values from the blockchain
App.todoList = await App.contracts.TodoList.deployed()
},
'''

Solved added var contract = required("truffle-contract");

Related

Error: getaddrinfo ENOTFOUND on Vercel with Next.JS and serverless-mysql

I am using Next.js and in the API side i have write a dummy code to get rows from my database with this library : serverless-mysql
I have followed the example on the documentation and on my computer this working very fine, I can connect to the database et get the rows. My Database is on my VPS not on my localhost.
But when i deploy my code on Vercel, and I try to access to /api/hello
In my vercel log I have this error :
[GET] /api/hello
{
error: Error: Error: getaddrinfo ENOTFOUND "**.***.**.**"
at connect (/var/task/node_modules/serverless-mysql/index.js:80:15)
at processTicksAndRejections (internal/process/task_queues.js:95:5)
at async Object.query (/var/task/node_modules/serverless-mysql/index.js:182:5)
at async excuteQuery (/var/task/.next/server/pages/api/hello.js:33:25)
at async handler (/var/task/.next/server/pages/api/hello.js:59:24)
at async Object.apiResolver (/var/task/node_modules/next/dist/server/api-utils.js:102:9)
at async Server.handleApiRequest (/var/task/node_modules/next/dist/server/next-server.js:1064:9)
at async Object.fn (/var/task/node_modules/next/dist/server/next-server.js:951:37)
at async Router.execute (/var/task/node_modules/next/dist/server/router.js:222:32)
at async Server.run (/var/task/node_modules/next/dist/server/next-server.js:1135:29)
}
(I have replaced the real Ip showed in the message by "** . *** . ** . **")
My database accept connection from outside because I can access to it on my computer.
I have also correctly configured the .env var in project settings.
Thank you very much for your help
You will need to set the environment variables both on your vercel dashboard and your nextjs app.
In your .env file
NEXT_PUBLIC_VERCEL_URL = "http://localhost:3000";
In your code, reference the variable
export const getBaseUrl = () => {
if (process.env.NODE_ENV === "development") {
return "http://localhost:3000";
}
return process.env.NEXT_PUBLIC_VERCEL_URL;
}
You can then execute the utility function anywhere in your code.
On vercel, set the environment variable to VERCEL_URL

Shopify API Node/Express Cannot read properties of undefined (reading 'Rest')

Just starting off with Shopify, and trying to get an order. Following the Shopify API documentation, here is my code:
const Shopify = require('#shopify/shopify-api');
const client = new Shopify.Clients.Rest('my-store.myshopify.com',
process.env.SHOPIFY_KEY);
module.exports.getShopifyOrderById = async (orderId) => {
return await client.get({
path: `orders/${orderId}`,
});
}
I get the following error when I execute this code:
TypeError: Cannot read properties of undefined (reading 'Rest')
Can't seem to figure out what the issue is.
You need to use Object destructing to get the Shopify object or use default export like below.
const { Shopify } = require('#shopify/shopify-api');
const client = new Shopify.Clients.Rest('my-store.myshopify.com',
process.env.SHOPIFY_KEY);
OR
const Shopify = require('#shopify/shopify-api').default;
const client = new Shopify.Clients.Rest('my-store.myshopify.com',
process.env.SHOPIFY_KEY);
OR
const ShopifyLib = require('#shopify/shopify-api');
const client = new ShopifyLib.Shopify.Clients.Rest('my-store.myshopify.com',
process.env.SHOPIFY_KEY);
This has to do with how ES6 modules are emulated in CommonJS and how you import the module. You can read about that here.

Firebase Cloud Function Keep return "Request failed with status code 404"

I created some functions in firebase cloud functions, but all of them are works. But i have a new function which is not work properly. I don't know why but i think it has same pattern with others.
this is my code:
const functions = require('firebase-functions');
const appVideo = express();
const cors = require('cors')({ origin: true });
appVideo.use(cors);
appVideo.get('/update-video', async(req, res) => {
console.log('updateStatusVideo idCourse', req.query.idCourse, ' idMateri: ', req.query.idMateri, ' idVideo:', req.query.idVideo);
res.status(200).send('Oke')
})
exports.video = functions.https.onRequest(appVideo)
I often call partial deploy like
firebase deploy --only functions:video. But when i execute the functions https through browser it often return
Request failed with status code 404
other weird things is when i inspect the browser and switch to console, i found
Failed to load resource: the server responded with a status of 500 ()
this is the url of function in firebase:
https://us-central1-my-apps.cloudfunctions.net/video [modified for confidential]
Please help
When you export this line:
exports.video = functions.https.onRequest(appVideo);
You define a Cloud Function called video that is deployed as https://us-central1-PROJECT_ID.cloudfunctions.net/video where PROJECT_ID is whatever your Firebase Project ID is.
Because you use a express application for this exported function, any URL that is handled must first start with https://us-central1-PROJECT_ID.cloudfunctions.net/video (the BASE_URL).
This line:
appVideo.get('/update-video', ...)
attaches a listener to BASE_URL/update-video, which becomes https://us-central1-PROJECT_ID.cloudfunctions.net/video/update-video.
If you want to use just https://us-central1-PROJECT_ID.cloudfunctions.net/video as-is, you'll need to change to using
appVideo.get('/', ...)
If you want to use just https://us-central1-PROJECT_ID.cloudfunctions.net/update-video, you'll need to change to using
appVideo.get('/', ...)
and
exports.update = {};
exports.update.video = functions.https.onRequest(appVideo);
// "update.video" is deployed as "update-video"
Note: This last part abuses deploying groups to get the desired URL

truffle: Error while creating instance of the contract - no code at address 0x3800e97896c6fdb614b9d011c9344dea32e49047

I have deployed a simple 'HelloWorld' contract on the ethereum node using truffle. I am trying to access and call a function inside the contract with a node API.
Below is the code:
var Web3 = require('web3');
var express = require('express');
const app = express();
app.use(express.json());
const artifact = require('./../ethereumapp/example/smartContract/build/contracts/HelloWorld.json')
const contract = require('truffle-contract');
const HelloWorldContract = contract(artifact);
var web3 = new Web3('http://localhost:8000');
HelloWorldContract.setProvider(web3.currentProvider);
var contractapp = HelloWorldContract.at('0x3800e97896c6fdb614b9d011c9344dea32e49047').catch((err)={
//Error gets thrown here
console.error("Error in creating instance of HelloWorld :"+err)}
);
app.get('/', (req, res)=>{
res.send("Hey! The app is up and running");
});
app.get('/getMessage',(req,res)=>{
console.log('Invoking smart contract...');
res.send(contractapp.getMessage());
});
app.listen(3000, ()=>{
console.log("App started on 3000");
});
I am new to ethereum/truffle and can't find out why this error is thrown. Please provide any sample nodejs code which can help me sort out this.
In my case this error occurred because the contract deployment transaction was running out of gas. Truffle's default gas limit used for deploys is 6721975 (reference: truffle general options).
Here's the help text that appeared for the deployment dry-run explanining the issue:
Error: *** Deployment Failed ***
"LinearLiquidityPool" ran out of gas. Something in the constructor (ex: infinite loop) caused gas estimation to fail. Try:
* Making your contract constructor more efficient
* Setting the gas manually in your config or as a deployment parameter
* Using the solc optimizer settings in 'truffle-config.js'
* Setting a higher network block limit if you are on a
private network or test client (like ganache).

Variable inside a class is not defined NodeJS

I am develping an application to send emails with nodeJs 6.9 and express 4.
I am also using a library called powerdrill wich is the library that calls Mandrill APIs.
I am trying to use the message variable which is the varible that use the powerdrill library to create the object and send the email. I am using this variable inside a method but is throwing an error.
'use strict';
var config = require('config'),
queries = require('api/queries'),
Message = require('powerdrill').Message;
class MandrillService{
constructor(config, Message) {
this.config = config;
this.Message = Message;
}
welcome(){
//console.log(req.body);
message = new Message();
message.apiKey(config.mandrillKey)
.subject('Welcom')
.template('welcome-email')
.from(config.mandrilEmail)
.to('david#gmail.com')
.tag('complex')
.globalMergeVar('VERIFY_EMAIL','link')
.send(function(err, resp) {
});
}
}
module.exports = new MandrillService(config);
error:
Unhandled rejection ReferenceError: message is not defined
at MandrillService.welcome (/home/user/platform/services/mandrillService.js:28:17)
at /home/user/platform/services/notificationService.js:222:22
at Promise._execute (/home/user//platform/node_modules/bluebird/js/release/debuggability.js:303:9)
at Promise._resolveFromExecutor (/home/user/platform/node_modules/bluebird/js/release/promise.js:483:18)
I donĀ“t know what is the behavior inside a class but if I try to call the method using express inside a post method, and using it like follows the code works fine.
var express = require('express'),
router = express.Router(),
config = require('config'),
Message = require('powerdrill').Message;
router.post('/welcome',function(req,res,next){
message = new Message();
message.apiKey(config.mandrillKey)
.subject(req.body.subject)
.template(req.body.template)
.from(config.mandrilEmail)
.to(req.body.to)
.tag('complex')
.globalMergeVar('VERIFY_EMAIL',req.body.linkVefifyEmail)
.send(function(err, resp)
res.send(resp).end();
});
});

Resources