Why does my code thing deployed() is not a function? - javascript

code:
loadContract: async ()=>{
//Create JS version of smart contract
const todoList = await $.getJSON('TodoList.json')
App.contracts.TodoList = TruffleContract(todoList)
App.contracts.TodoList = web3.setProvider(new Web3.providers.HttpProvider("http://127.0.0.1:7545"))
//getting values from blockchain
App.todoList = await App.contracts.TodoList.deployed()
console.log(todoList)
error:
TypeError: App.contracts.TodoList.deployed is not a function
I am able to call TodoList.deployed() in the truffle dev environment but not in the JavaScript file. I am trying to to set all the data from the deployed smart contract to App.todoList so I can work with the values in the JavaScript file and render them to the HTML. I am completely stuck. When I console.log(todolist) I receive the JSON of the smart contract.
Here is my migrations file.
//need migration to update state of blockchain
const TodoList = artifacts.require("./TodoList.sol");
module.exports = function (deployer) {
deployer.deploy(TodoList);
};
//run migration and deploy contract to blockchain

Related

Unable to use Datastore emulator with Nodejs application

I am trying to use datastore emulator with my nodejs application
Initially I have followed the instructions given in here
Then in my node application :-
var config = {
projectId : "scio1-ts-datastore"
}
const datastore = require("#google-cloud/datastore")
const db = new datastore.Datastore(config)
const setdata = async () => {
await db.save(({
key : db.key('orders') ,
data : {
orderId : 1 ,
orderType : "Hazardous"
}
}))
}
const getdata = async () => {
const query = db.createQuery('orders')
const [orders] = await db.runQuery(query)
console.log(orders)
}
setdata()
getdata()
After this I had to run gcloud auth application-default login which I think is not needed to run as I am using datastore Emulator .
But even after this I am unable to run the app
Following error pops up :-
Not sure if this is the same issue, but for me a problem was that nodejs client did not "listen" to the environmental variables I set up (as part of the instructions you linked) and I had to initialize the datastore with endpoint explicitly given:
this.datastore = new Datastore({
apiEndpoint: "0.0.0.0:8081"
});
or whichever port the emulator says it's listening on.
Also make sure that the ~/.config/gcloud directory contains the application_default_credentials.json - unsure if emulator really needs it, but it worked for me after these steps.
Edit: if you are running the app through webstorm or such, it's good to note that environmental variables are loaded when you start it. So if it's already running when you set the variables, restart it!

Accessing a private Google sheet with a google cloud service account

I have a private Google Spreadsheet and I’m trying to access it programmatically using Google Visualization/Google Charts. I've created the service account, and I have tried using the google-auth-library and googleapis npm packages to create an access token that I can then use to access the spreadsheet. But, when I try to use that access token to read from the spreadsheet, the request fails with HTTP code 401 (Unauthorized). What do I need to do in order to make this work?
This is my code:
const { auth } = require('google-auth-library');
const keys = require('./jwt.keys.json');
const id = '{Spreadsheet ID}';
const sheetId = '{Sheet ID}';
const query = "{Query}";
async function getClient(){
const client = auth.fromJSON(keys);
client.scopes = ['https://www.googleapis.com/auth/spreadsheets.readonly'];
console.log(client);
return client;
}
async function main(){
const client = await getClient();
const token = await client.getAccessToken();
console.log(token);
console.log(token.token);
const url = `https://docs.google.com/spreadsheets/d/${id}/gviz/tq?tqx=out:csv&tq=${encodeURIComponent(query)}&access_token=${token.token}#gid=${sheetId}`;
const res = await client.request({url});
console.log(res);
}
main().catch(console.error);
When I saw your script, I thought that it is required modifying the scope. I had got the same situation as you (the status code 401 using your endpoint). Unfortunately, it seems that your endpoint cannot be used using the scope of https://www.googleapis.com/auth/spreadsheets.readonly. So, for example, how about changing it as follows, and testing it again?
From:
https://www.googleapis.com/auth/spreadsheets.readonly
To:
https://www.googleapis.com/auth/spreadsheets
or
https://www.googleapis.com/auth/drive.readonly
Note:
When I tested your endpoint using the modified scope, I confirmed that no error occurred. But if you tested it and when an error occurs, please check other part, again.

FirebaseError: [code=invalid-argument]: Function setDoc() called with invalid data

I am using Firebase SDK's within Node-red (as specified in NPM docs they can be used for IoT devices with NODE.js).
I can use all of the CRUD methods with Firebase RealtimeDatabase.
With Firebase Firestore I can only use READ and DELETE functionality.
SET and UPDATE results in weird errors that I couldn't find answers
anywhere on the internet.
I am importing Firebase SDK's through require() inside settiings.js and functionGlobalContext so I can access them in Node-red functions:
functionGlobalContext: {
firebase: require('firebase/app'),
firebaseDatabase: require('firebase/database'),
firebaseFirestore: require('firebase/firestore'),
// os:require('os'),
// jfive:require("johnny-five"),
// j5board:require("johnny-five").Board({repl:false})
},
First I initialize my whole Firebase project with this code (and everything initializes fine without errors):
//Load data from Global contexta
const firebase = global.get('firebase');
const firebaseDatabase = global.get('firebaseDatabase');
const firebaseFirestore = global.get('firebaseFirestore');
const firebaseConfig = {
//my Firebase credentials
};
//Set up Firebase
const app = firebase.initializeApp(firebaseConfig);
const database = firebaseDatabase.getDatabase();
const firestore = firebaseFirestore.getFirestore();
//Save the database reference to Global context
global.set('app', app);
global.set('database', database);
global.set('firestore', firestore);
And here I am trying basic SET operation with Firestore:
const ft = global.get('firebaseFirestore');
const firestore = global.get('firestore');
const frankDocRef = ft.doc(firestore, "users", "frank");
await ft.setDoc(frankDocRef, {
name: "Frank",
age: 12
});
Unfortunately even though this code is ctrl+c ctrl+v from Firestore docs I get this error:
"FirebaseError: [code=invalid-argument]: Function setDoc() called with invalid data. Data must be an object, but it was: a custom Object object (found in document users/frank)"
When I use the same code inside a web app everything works fine.
There has to be something going on under the hood with Node-red
I tried creating the object using various methods and all of them resulted in the same error.
Does anybody have any idea what could be going wrong here?

Firebase emulator wont run

I'm using the Firebase emulator in order to test my Cloud Function. The code I'm trying to execute is:
import * as functions from "firebase-functions";
const fetch = require('node-fetch');
export const newZipCode = functions.database.ref('/zipCodes/{zipCode}').onCreate(async (snapshot, context) => {
const api = await fetch(`https://api.papapi.se/lite/?query=${context.params.zipCode}&format=json&apikey=xxx`);
const json = await api.json();
console.log(context.params.zipCode);
if (api.ok) {
snapshot.ref.update({
latitude: json.results[0].latitude,
longitude: json.results[0].longitude
});
} else {
console.log(${api.status}));
}
});
However, when I try to run the Firestore- and Functions emulators, I receive this error message:
"Missing expected firebase config value databaseURL, config is actually{"storageBucket":"xxxx-yyyy.appspot.com","projectId":"xxxx-yyyy"}"
Shouldn't Cloud Functions and Firestore emulators be able to communicate right of the box? If not, what needs to be done in order to get the emulators running? FYI, I'm running Node.js 14 and Firebase version 9.6.1.
The problem is that you are starting the Firestore emulator when you should start the Database emulator instead, as you are using the Realtime Database onCreate() event handler.
Since you are starting the Firestore emulator, you probably don't have a Realtime Database created in the Firebase console so you get the "Missing expected firebase config value databaseURL..." error. You can create one following the steps of the public documentation.
Or if you want to use Cloud Firestore you need to modify your code to use the Firestore onCreate() event handler instead.
import * as functions from "firebase-functions";
const fetch = require('node-fetch');
export const newZipCode = functions.firestore.document('/zipCodes/{zipCode}').onCreate(async (snapshot, context) => {
const api = await fetch(`https://api.papapi.se/lite/?query=${context.params.zipCode}&format=json&apikey=xxx`);
const json = await api.json();
console.log(context.params.zipCode);
if (api.ok) {
snapshot.ref.update({
latitude: json.results[0].latitude,
longitude: json.results[0].longitude
});
} else {
console.log(${api.status}));
}
});

displaying scraped data from puppeteer onto a react app

I have a shopping cart app I developed using React, I have been working on a feature which scrapes data from a grocery store using puppeteer. The problem I am having is that im not sure of how to go about and share the data from my puppeter.js file and implement the data, and display it in my react app.
Any help is appreciated.
You need to get the data and save it to the local folder in a json format. Then you can use it in your react app. For example
const puppeteer = require("puppeteer")
const fs = require("fs")
async function scrapeData () {
const browser = await puppeteer.launch()
const [page] = await browser.pages()
/*
I dont know how your data is stored or what data you want to get so i cant
implement
getting the data here. Assume that i got it and stored in a variable named
'theData'
*/
fs.writeFile("theData.json", JSON.stringify(theData), () => {
console.log("The data saved to the file named theData.json")
})
}
scrapeData()
Then you can use fs.readFile() method to read and use it in your react app.

Categories