[SOLVED] Error: “message”: “data is not defined”, “code”: 141

I’m getting this error, but not sure why:

2022 - 09 - 01 T22: 09: 01.146 Z - afterSave failed for AuctionCreated for user:
Input: {
“log_index”: 238,
“transaction_hash”: “0xd0b76e0b490ba96611c6598db62907701c80d253665a7647289b5281bf84027e”,
“createdAt”: “2022-09-01T22:05:11.907Z”,
“updatedAt”: “2022-09-01T22:09:01.116Z”,
“address”: “0x55e0f7a3bb39a28bd7bcc458e04b3cf00ad3219e”,
“block_hash”: “0xd624f6684b4e65ede6367a018454ad1997f0d3bc82aa30877a1ff814eccb0359”,
“block_number”: 15455394,
“block_timestamp”: {
“__type”: “Date”,
“iso”: “2022-09-01T22:04:39.000Z”
},
“confirmed”: true,
“endTime”: “1662070779”,
“endTime_decimal”: {
“__type”: “NumberDecimal”,
“value”: “1662070779”
},
“nounId”: “5519”,
“nounId_decimal”: {
“__type”: “NumberDecimal”,
“value”: “5519”
},
“startTime”: “1662069879”,
“startTime_decimal”: {
“__type”: “NumberDecimal”,
“value”: “1662069879”
},
“transaction_index”: 131,
“objectId”: “d4A4GTwbYsnw1Fhf4yQ1drUP”
}
Error: {
“message”: “data is not defined”,
“code”: 141
}

This is the cloud function (based on the one from the Moralis video tutorial):

const logger = Moralis.Cloud.getLogger();
logger.info("Hello World");

Moralis.Cloud.afterSave("AuctionCreated", async (request) => {

  const confirmed = request.object.get("confirmed");
  const nounId = request.object.get("nounId");
  
  let msg = `New Auction Started for Lilnoun ${nounId}`;
  if(confirmed){
    let data = {
      app_id: "***********************************",
      contents: {"en": "Notification"},
      included_segments: ["Subs Email"],
      name: "Email",
      email_body: msg,
      email_subject: "New Auction"
    }
  }
  
  Moralis.Cloud.httpRequest({
    method: "POST",
    url: "https://onesignal.com/api/v1/notifications",
    body: data,
    headers: {
    'Content-Type': 'application/json',
    'Authorization': 'Basic **************************************************'
    }
  })
  })

Server:
https://witiy4l01xiz.usemoralis.com:2053/serverPreformatted text

Can you read this on how to post code.

In this case, data is defined inside the scope of your if statement, so it doesn’t exist when you try to pass it into your httpRequest.

You can just do let data at the top of your cloud function and update it later with data = { }.

OK, took out the if statement and did it like this, but it still get the same error.

const logger = Moralis.Cloud.getLogger();
logger.info("Hello World");

Moralis.Cloud.afterSave("AuctionCreated", async (request) => {

  const confirmed = request.object.get("confirmed");
  const nounId = request.object.get("nounId");
  
  let msg = `New Auction Started for Lilnoun ${nounId}`;


  let data = {
    app_id: "*********************************",
    contents: {"en": "Notification"},
    included_segments: ["Subs Email"],
    name: "Email",
    email_body: msg,
    email_subject: "New Auction"
  }
  
  
  Moralis.Cloud.httpRequest({
    method: "POST",
    url: "https://onesignal.com/api/v1/notifications",
    body: data,
    headers: {
    'Content-Type': 'application/json',
    'Authorization': 'Basic ***********************************************'
    }
  })
  })`Preformatted text`

same exact error now?
you could add more logging to see at what line it give the error

Looks like it works now, thanks