trying to use the following code in a custom code block..
test invokation gets an error code 400
error body:
{
“code”: 0,
“message”: “Cannot read properties of undefined (reading ‘SHA256’)”,
“errorData”: {}
}
the code i presume that caused it: (no idea how undefined though)
found that there is no Backedless.CryptoUtils and no Backedless.Crypto to do SHA256 hash with also my codeless blocks contain logging blocks but the logs are empty
You’re correct - Backendless does not provide a built-in Backendless.CryptoUtils or Backendless.Crypto object for cryptographic operations in custom code blocks (Cloud Code, API Services, or Event Handlers). That’s why you’re seeing the Cannot read properties of undefined (reading ‘SHA256’) error.
I tried using those codes but it didn’t put anything in the database except toggled PAID to be true… (though i think this might be a Codeless cached endpoint/webhook issue) SERIAL remains empty… and the log block i put in before the custom code never got to the RT logging page..
I’m starting to think i can’t do sha256 string hash without a codeless block for it.. though have been told i can use code, just the codeless blocks appear to only do part of it… heres my Custom Code:
const email = EMAIL;// use custom_id
// ✅ Fetch secret from Backendless env variables?
const secret = "tet"+"match"+"index";
if (!secret) {
throw new Error("LICENSE_SECRET not configured");
}
// Normalize email to match game logic
const cleanEmail = email.toLowerCase().trim();
const crypto = require('crypto');
function generateLicenseKey(cleanEmail, secret) {
const raw = cleanEmail + secret;
const hash = crypto.createHash('sha256').update(raw).digest('hex');
const license = hash.substring(0, 20).toUpperCase();
return license;
}
const license = await generateLicenseKey(cleanEmail, secret);
// Generate license key
//const raw = cleanEmail + secret;
//const hash = sha256(raw);
//const license = HASH.substring(0, 20).toUpperCase();
console.log("License:", license);
// Save to Backendless table "Licenses"
//const licenseObj = {
// PAID: true,
// email: cleanEmail,
// SERIAL: license,
// createdAt: new Date().toISOString()
//};
//Backendless.Data.of("Users").save(licenseObj);
try {
var queryBuilder = Backendless.DataQueryBuilder.create()
.setWhereClause(`email = '${cleanEmail}'`);//AND PaymentMade = true);
var existingUser = Backendless.Data.of('Users').findFirst(queryBuilder);
if (existingUser.PAID == false){
console.log(`PAID = `,existingUser.PAID);
existingUser.PAID = true;
existingUser.SERIAL = license;
var result = Backendless.Data.of('Users').save(existingUser);
console.log(`Found in result`,result);
if (result && email) {
console.log(`Found in Users Table`, result);
//return result; // Return first match found
}
}else{
//display already paid
//return existingUser;
}
} catch (error) {
if (error.code && error.code !== 1009) { // 1009 means "no data found"
console.error(`Error querying table:`, error);
//return null; // Stop lookup on unexpected error
}
}
//Backendless.Data.of('Users').bulkUpdate( (String('email = ') + String(cleanEmail)), ({ [`PaymentMade`]: true,[`SERIAL`]: license }) );
// ✅ TODO: send email with license
const subject = "Your Bouncy License Key";
const bodyText = `Hello,
Thank you for your purchase!
Here is your license key (use it with your Bouncy email address to activate):
${license}
Best regards,
Bouncy Dev - JesterOC`;
Backendless.Messaging.sendEmail(subject, bodyText, [cleanEmail]);
return { status: "ok", license };
//}
return { status: "ignored" };
Please help. EDIT: forgot to pass the email into the Custom Code, so that wasn’t the Codeless Service that is marking user as paid… no errors though..
EDIT2: it’s as if the code only partly works…
i was able to put the return result from the custom code in the logging text and got
{“status”:“ok”,“license”:“354B4784CD**********”} added stars (*) to omit serial value
but it didn’t get paid or serial in the database… and the part to email the user doesn’t work and gives no errors
We’re sorry you’ve run into this issue. To help us investigate, please share your appId and, if possible, a minimal test page that reproduces the problem, along with the exact steps to reproduce. This will let us test safely without touching your live app or data. You might also try running the flow with mock data, add step-by-step logging around the functions and other operations involved, and double-check the schema of the table you’re writing to. These details—and the exact steps to reproduce—will make it much easier for us to pinpoint the problem and suggest a fix.
APPLICATION ID IS FF55A111-06AF-7EB1-FFDE-B5B62760CA00
STEPS TO REPRODUCE…
GO TO API SERVICES
Navigate to
paymentWithinAccount > payment (the operation).
Test Invoke with email in custom_id parameters
What happens is that it runs part of the code and the rest doesn’t even give a log message… gets a serial, but dismisses user data update and the serial email.
Thank you for the information you provided — I’ve checked and identified the causes of the issues.
When updating the Users table, the whereClause was in the wrong format, so the update didn’t happen. The correct format is:
email = '${cleanEmail}'
The email wasn’t sent because the second parameter needs to be bodyParts, not a plain string. Here’s the correct usage and a link to the documentation:
const bodyParts = new Backendless.Bodyparts();
bodyParts.textmessage = bodyText;