No result is happening for this Web3.js call - javascript

const fs = require('fs')
const web3 = new Web3("ws://localhost:7545");
const contract_address = "0x7484d32e8911817702c5d7c764dBF7e592000b415";
async function web3Contract() {
const contract_abi = fs.readFileSync('./build/contracts/Bottle.json', 'utf8')
const abi = JSON.parse(contract_abi).abi;
// console.log(abi);
const Bottle = await new web3.eth.Contract(abi, contract_address);
const accounts = await web3.eth.getAccounts();
await Bottle.methods.setName("Palm").send({from:accounts[0]});
const greeting = await Bottle.methods.getGreeting().call();
console.log(greeting);
});
}
async function run() {
try {
await web3Contract();
} catch (err) {
console.log('Your error is this - ' , err);
} finally {
console.log('finally');
}
}
run();
getGreeting().call() is giving me this error. I tried numerous different ways and been stuck on here for hours. Not to sure what to do. https://gyazo.com/81970d03c1380cd513998f25deef9e40

You're incorrectly combining await and the callback function (in your case function(result) {}).
Since you're using await, it never reaches the callback function, and the returned Promise is resolved by the await statement.
In the context of your code, it makes more sense to return the value from the await and then print it.
// remove the callback function
const greeting = await Bottle.methods.getGreeting.call();
console.log(greeting);

Related

How to use async/await using crypto.randomBytes in nodejs?

const crypto = require('crypto');
async function getKey(byteSize) {
let key = await crypto.randomBytes(byteSize);
return key;
}
async function g() {
let key = await getKey(12);
return key;
}
console.log(g());
console.log('hello - want this called after g() above');
I've been at this for an hour and I can't understand how to ensure that I get a key using async/await. I get a Promise-pending no matter what I do.
I've also tried this:
async function getKey(byteSize) {
let key = await crypto.randomBytes(byteSize);
return key;
}
getKey(12).then((result) => { console.log(result) })
console.log('hello');
... to no avail! Which was inspired by:
How to use await with promisify for crypto.randomBytes?
Can anyone help me with this?
All I'm trying to do is to get randomBytes async. using the async./await block but ensure that it fulfills the promise before I carry on in the code.
This is an extension of my comment on the question
Since you're not promisify'ing or passing a callback to crypto.randomBytes() it is synchronous so you can't await it. Additionally, you're not properly awaiting the promise returned by g() at the top level. That is why you always see the pending Promise in your console.log()
You can use util.promisify() to convert crypto.randomBytes() into a promise returning function and await that. There is no need for the async/await in your example because all that is doing is wrapping a promise with a promise.
const { promisify } = require('util')
const randomBytesAsync = promisify(require('crypto').randomBytes)
function getKey (size) {
return randomBytesAsync(size)
}
// This will print the Buffer returned from crypto.randomBytes()
getKey(16)
.then(key => console.log(key))
If you want to use getKey() within an async/await style function it would be used like so
async function doSomethingWithKey () {
let result
const key = await getKey(16)
// do something with key
return result
}
If the callback function is not provided, the random bytes are generated synchronously and returned as a Buffer
// Synchronous
const {
randomBytes
} = await import('node:crypto');
const buf = randomBytes(256);
console.log(
`${buf.length} bytes of random data: ${buf.toString('hex')}`);
const crypto = require('crypto');
async function getKey(byteSize) {
const buffer = await new Promise((resolve, reject) => {
crypto.randomBytes(byteSize, function(ex, buffer) {
if (ex) {
reject("error generating token");
}
resolve(buffer);
});
}
async function g() {
let key = await getKey(12);
return key;
}
const crypto = require("crypto");
async function getRandomBytes(byteSize) {
return await new Promise((resolve, reject) => {
crypto.randomBytes(byteSize, (err, buffer) => {
if (err) {
reject(-1);
}
resolve(buffer);
});
});
}
async function doSomethingWithRandomBytes(byteSize) {
if (!byteSize) return -1;
const key = await getRandomBytes(byteSize);
//do something with key
}
doSomethingWithRandomBytes(16);

Multiple fetchs in generator function

as I'm trying to learn how to use generator functions, I've run into a problem using it to run multiple fetchs.
What I'm trying to do:
I'm trying to use the Hackernews API to fetch a newsstory, and then afterwards, fetching a comment from the same API, using the id given in the response.
Output:
{ value: Promise { }, done: false }
TypeError: Cannot read property 'kids' of undefined
at getFirstComment (C:\Users\madsn\Documents\dev\generatorfunctions\fetchingInterTwice.js:13:29)
at getFirstComment.next ()
at runThroughIt (C:\Users\madsn\Documents\dev\generatorfunctions\fetchingInterTwice.js:31:35)
at processTicksAndRejections (internal/process/task_queues.js:93:5)
{ value: undefined, done: true }
{ value: undefined, done: true }
Code:
const fetch = require("node-fetch")
function requestStuff(url) { ///simple request
return fetch(url).then(respJson => respJson.json()).catch(err => console.log(err));
}
function* getFirstComment() {
try {
//gets the story and the comments id(kids of type array)
var result1 = yield requestStuff("https://hacker-news.firebaseio.com/v0/item/8863.json?print=pretty")
var resID = result1.kids[0];
//gets the comment-text given the comment-id
var result2 = yield requestStuff("https://hacker-news.firebaseio.com/v0/item/" + resID + ".json?print=pretty")
var resText = result2.text
console.log(resText.text)
} catch (error) {
console.log(error)
}
}
let main = getFirstComment()
async function runThroughIt(generator) {
let stepOne = await generator.next()
console.log(stepOne)
let stepTwo = await generator.next()
console.log(stepTwo)
let stepThree = await generator.next()
console.log(stepThree)
}
runThroughIt(main)
Thanks in advance if you have a fix or the link to an article explaining this!
Here are the issues with your code:
Your requestStuff function should be async and the return value should be the evaluated result from the network request. Currently, you're simply returning the promise object.
The yield keyword will "kick" you out of the function BEFORE the result from the fetch expression is assigned to result1.
Here is how you could fix it:
const fetch = require("node-fetch")
async function requestStuff(url) { ///simple request
const res = await fetch(url)
.then(respJson => respJson.json())
.catch(err => console.log(err));
return res;
}
async function* getFirstComment() {
try {
//gets the story and the comments id(kids of type array)
var result1 = await requestStuff("https://hacker-news.firebaseio.com/v0/item/8863.json?print=pretty");
yield result1;
var resID = result1.kids[0];
//gets the comment-text given the comment-id
var result2 = await requestStuff("https://hacker-news.firebaseio.com/v0/item/" + resID + ".json?print=pretty")
var resText = result2.text
console.log(resText.text)
yield resText;
} catch (error) {
console.log(error)
}
}
let main = getFirstComment()
async function runThroughIt(generator) {
let stepOne = await generator.next();
console.log("res from step 1! ", stepOne);
let stepTwo = await generator.next()
console.log("res from step 2: ", stepTwo);
//will return undefined because the function has finished evaluating!
let stepThree = await generator.next()
console.log(stepThree)
}
runThroughIt(main)
The above works!
Now pay special attention to where the yield, await and async keywords are being called in my example and compare it with where you called it in your code!

NodeJS Express app await is only valid in async function but this clearly is async function?

I have made a function to check if a certain thing already exists in the database. I have simply copy-pasted the logic I'm using to get something in the database and changed the query object + what is returned. But now it seems that node doesn't like that and just throws an error that makes no sense to me.
Where I call the function:
let exists = await queryDatabaseExists(uniqueQuery, res);
The function that I'm calling:
async function queryDatabaseExists(queryParam, res) {
try {
const cp = new sql.ConnectionPool(config);
await cp.connect();
let result = await cp.request().query(queryParam);
if(result.recordset.rowsAffected[0] = 1){return true} else { return false }
} catch (err) {
res.status(520).send(`Database error: ${err}`);
}
}
The error that I'm getting:
let exists = await queryDatabaseExists(uniqueQuery, res);
^^^^^
SyntaxError: await is only valid in async function
ALL code for that route:
router.post("/admin/category", (req, res) => {
uniqueQuery = `SELECT [name] from [dbo].[idtTV_categories] WHERE [name] = '${req.body.name}'`
getQuery = `SELECT [id]
,[name]
,[description]
,[created_time]
,[created_by] from [dbo].[idtTV_categories]`
standardQuery = `INSERT INTO [dbo].[idtTV_categories] ([name],[description],[created_time],[created_by])
VALUES
('${req.body.name}',
'${req.body.description}',
SYSDATETIME(),
'${req.user.name}')`;
let exists = checkIfExists();
function checkIfExists() { result = await queryDatabaseExists(uniqueQuery, res); return result} ;
console.log(exists);
if(req.user.roles.some(role => role === admin || role === editor)){
if(!existsInDatabase){
if(queryDatabase(standardQuery, res)){queryDatabase_get(getQuery, res)}
}
}
else { res.statusMessage = `${req.user.name} is not authorized to add categories.`;
console.log(req.user.roles)
res.status(520).send() };
})
All functions being called:
///////////// MAIN QUERYING FUNCTION //////////////////////
async function queryDatabase_get(queryParam, res) {
try {
const cp = new sql.ConnectionPool(config);
await cp.connect();
let result = await cp.request().query(queryParam);
res.send(result.recordset);
} catch (err) {
res.status(520).send(`Database error: ${err}`);
}
}
async function queryDatabaseExists(queryParam, res) {
try {
const cp = new sql.ConnectionPool(config);
await cp.connect();
let result = await cp.request().query(queryParam);
if(result.recordset.rowsAffected[0] = 1){return true} else { return false }
} catch (err) {
res.status(520).send();
}
}
async function queryDatabase(queryParam, res) {
try {
const cp = new sql.ConnectionPool(config);
await cp.connect();
let result = await cp.request().query(queryParam);
if(result.rowsAffected > 0){ return true }
} catch (err) {
res.status(520).send(`Database error: ${err}`);
}
}
it must be inside async.
ex:
app.post('/', async (req, res) => {
let exists = await queryDatabaseExists(uniqueQuery, res);
});
This means that the function in which the call to queryDatabaseExists is performed must also be async, in order to use the await keyword inside it. The queryDatabaseExists function itself looks correct.
queryDatabaseExists function need to return promise if not you cannot use await
await command expect a promise to be return by the function link
let exists = await queryDatabaseExists(uniqueQuery, res);
await can only be used in async function. For your scenario you wanted wait for result from uniquequery. First you have to make change in your router.post callback like router.post('/url',async(req,res)=>{}); for making a synchornus call to checkifexist function. Second in order to use await in checkifexist function you have to make changes to checkisexist function to async function checkifexist(){}. Thirdly you wanted to wait for DB response for that you have use await while calling checkifexist function --> let result=await checkifexist(). you can check MDN website for better understanding.
router.post('url',async(req,res)=>{// in order to use await in checkifexist
//your rest of the code.
let result=await checkifexist();// waiting for db result
async function checkifexist(){//your awaited code.}
console.log(result);
});

Execute code after fs.writeFile using async/await

I have a function, startSurvey, which, when run, checks if there are questions in a .json file. If there are no questions, it fetches some questions from Typeform and writes them to the .json file using saveForm. After it writes, I would like to continue executing some code that reads the .json file and logs its contents. Right now, await saveForm() never resolves.
I have promisified the fs.readFile and fs.writeFile functions.
//typeform-getter.js
const fs = require('fs')
const util = require('util')
const fetch = require('cross-fetch')
require('dotenv').config()
const conf = require('../../private/conf.json')
const typeformToken = conf.tokens.typeform
const writeFile = util.promisify(fs.writeFile)
const getForm = async () => {
const form = await fetch(`https://api.typeform.com/forms/${process.env.FORM_ID}`, {
headers: {
"Authorization": `bearer ${typeformToken}`
}
}).then(res => res.json())
const fields = form.fields
return fields
}
const saveForm = async () => {
const form = await getForm()
return writeFile(__dirname + '/../data/questions.json', JSON.stringify(form))
.then((e) => {
if (e) console.error(e)
else console.log('questions saved')
return
})
}
module.exports = saveForm
//controller.js
const fs = require('fs')
const util = require('util')
const request = require('request')
require('dotenv').config()
const typeformGetter = require('./functions/typeform-getter')
const readFile = util.promisify(fs.readFile)
const saveForm = util.promisify(typeformGetter)
let counter = 1
const data = []
const getQuestions = async() => {
console.log('called')
try {
let data = await readFile(__dirname + '/data/questions.json')
data = JSON.parse(data)
return data
} catch (e) {
console.error('error getting questions from read file', e)
}
}
const startSurvey = async (ctx) => {
try {
const questions = await getQuestions()
if (!questions) await saveForm()
console.log(questions) //NEVER LOGS
} catch (error) {
console.error('error: ', error)
}
}
startSurvey() //function called
I don't know your exact error, but there are multiple things wrong with your code:
You're using incorrectly the promisified version of fs.writeFile, if an error occurs, the promise will be rejected, you won't get a resolved promise with an error as the resolved value, which is what you're doing.
Use path.join instead of concatenating paths.
In startSurvey, you're using console.log(questions) but that wont have any data if questions.json doesn't exists, which should happen the first time you run the program, since it's filled by saveForm, so you probably want to return the questions in saveForm
So saveForm should look something like this:
const saveForm = async () => {
const form = await getForm();
const filePath = path.join(path.__dirname, '..', 'data', 'questions.json');
await writeFile(filePath, JSON.stringify(form));
console.log('questions saved');
return form;
}
And startSurvey
const startSurvey = async (ctx) => {
try {
const questions = await getQuestions() || await saveForm();
// This will be logged, unless saveForm rejects
// In your code getQuestions always resolves
console.log(questions);
} catch (error) {
console.error('error: ', error)
}
}
In your controller.js you're using util.promisify on saveForm when it is already a promise.
So it should be:
const saveForm = require('./functions/typeform-getter')

Using await in then callback - the keyword 'await' is reserved

In node.js, I have a database transaction, where I want to call an async method in then callback, but I get error message the keyword 'await' is reserved.
This is async saveImage function:
const saveImage = async (parsedLink) => {
AWS.config.region = config.awsSettings.region;
AWS.config.accessKeyId = config.awsSettings.accessKeyId;
AWS.config.secretAccessKey = config.awsSettings.secretAccessKey;
const bucket = new AWS.S3({
params: {
Bucket: config.awsSettings.images_bucket_name,
},
});
const currentDateString = new Date().toISOString().replace(/\:|\./g, '-');
const bodystream = new Buffer(parsedLink.imgUrl, 'binary');
const imageUrlDomain = parseDomain(parsedLink.linkUrl).domain;
const params = {
Key: `${parsedLink.id}/${imageUrlDomain}_${currentDateString}${parsedLink.imgType}`,
ContentType: parsedLink.imageMime,
ContentEncoding: 'base64',
Body: bodystream,
};
const resultPromise = await bucket.upload(params).promise();
return resultPromise.Location;
};
If I want to use saveImage function, I get the error message.
module.exports.addTestObject = async (ctx) => {
const testObj = ctx.request.body;
try {
switch (testObj.type) {
case interestT.getType.link: {
const knexTestObject = TestObject.knex();
transaction(knexTestObject, trx =>
TestObject.query(trx)
.insert({
interestDate: testObj.date,
})
.then(newInterest => {
// save image
if (parsedLink.saveImg) {
parsedLink.imgUrl = await saveImage(testObj);
}
newInterest.$relatedQuery('linkInterestsRel', trx).insert({
linkHeader: testObj.linkHeader,
}),
}
),
)
.then((linkInterest) => {
console.log(linkInterest);
})
.catch((err) => {
throw err;
});
break;
}
default:
break;
}
ctx.response.body = interestObj;
} catch (err) {
const statusCode = err.status || 400;
ctx.throw(statusCode, err.message);
}
};
Regular functions run synchronously till they return. Therefore you cannot use await inside them as you cannot wait for an asynchronous event in a synchronous way.
JavaScript also has async functions, which look like regular functions, but are conceptually quite different: They run synchronously till they reach an await, then they stop and continue once the awaited Promise resolves. As such they cannot return their result synchronously, instead they return a Promise which then resolves when the function finished execution.
Therefore you need to convert your function into an async function:
async function getUsername() { // <-- async keyword here
return (await getUser()).name; // <-- await can be used inside
}
Now this does also work inside a .then callback:
getUser().then(async function(user) {
const friends = await getFriends(user);
// ...
})
But this somewhat mixes the abstraction async functions with their underlying primitive Promise. If you would just await the Promise instead of adding a .then callback, the code gets way more readable:
(async function() {
const user = await getUser();
const friends = await getFriends(user);
})();
The concrete question could be rewritten as:
const linkInterest = await transaction(knexTestObject, async trx => {
const newInterest = await TestObject.query(trx)
.insert({ interestDate: testObj.date, });
if (parsedLink.saveImg) {
parsedLink.imgUrl = await saveImage(testObj);
}
await newInterest.$relatedQuery('linkInterestsRel', trx)
.insert({ linkHeader: testObj.linkHeader, }),
});

Categories