'No idea why I have this error TypeError: card.build is not a function google apps script cardservice

For the life of me I cant figure out why I'm getting this error: "TypeError: card.build is not a function [line: 40, function: receiptCardOnOpen, file: receipt card]" This code was stable then I started to add a second card then it started to throw the above error. I removed all new code restoring it back to its stable version but the error persists. Any help would be much appreciated cheers.

The following code snippet is what was working


/**
 * @param {Object}
 * @return {CardBuilder} 
 */
function receiptCard(prefills) {
  Logger.log("receiptCard(prefills) called")
  
  return CardService.newCardBuilder()
    .setHeader(
      CardService.newCardHeader()
        .setTitle("Enter Receipt")
    )
    .addSection(formSection(prefills))
    
    
}
/**
 * Returns the contextual add-on data that should be rendered for
 * the current e-mail thread. This function satisfies the requirements of
 * an 'onTriggerFunction' and is specified in the add-on's manifest.
 *
 * @param {Object} event Event containing the message ID and other context.
 * @returns {Card[]}
 */
function receiptCardOnOpen(event) {
  Logger.log("receiptCardOnOpen(event)")
  var message = getCurrentMessage(event);
  //Logger.log(event)
  let prefills = {
                  receivedDate:getReceivedDate(message),
                  cardType:findCardType(message),
                  amounts:findDollarAmounts(message),
                  attachments:getAttachment(message),
                  receiptId:ATL.getUUID(),
                  ref:hasRef(message)
                  }
                  //Logger.log(prefills)
  let card = receiptCard(prefills)
  return [card.build()]
}

The first thing I did was replace return [card.build()] with return [CardService.newCardBuilder().setHeader(CardService.newCardHeader().setTitle("test")).build()] and it worked.

The next thing I tried was replace the contents of "receiptCard(prefills)"

function receiptCard(prefills) {
  Logger.log("receiptCard(prefills) called")
  
  return CardService.newCardBuilder()
    .setHeader(
      CardService.newCardHeader()
        .setTitle("Enter Receipt")
    )
    .addSection(formSection(prefills))
}

with

/**
 * @param {Object}
 * @return {CardBuilder} 
 */
function receiptCard(prefills) {
  Logger.log("receiptCard(prefills) called")
    return CardService.newCardBuilder().setHeader(CardService.newCardHeader().setTitle("test"))
    
}

and it fails with the same error. This leads me to believe the error is happening before this part

.addSection(formSection(prefills)) 

Is this a far assessment?
The next thing I tried was wrapping

return CardService.newCardBuilder().setHeader(CardService.newCardHeader().setTitle("test"))

in a new function like so

function testError(){
  return CardService.newCardBuilder().setHeader(CardService.newCardHeader().setTitle("test"))
}

and made this change in receiptCardOnOpen(event) let card = receiptCard(prefills)tolet card = testError() and now "receiptCardOnOpen(event)" looks like

function receiptCardOnOpen(event) {
  Logger.log("receiptCardOnOpen(event)")
  var message = getCurrentMessage(event);
  //Logger.log(event)
  let prefills = {
                  receivedDate:getReceivedDate(message),
                  cardType:findCardType(message),
                  amounts:findDollarAmounts(message),
                  attachments:getAttachment(message),
                  receiptId:ATL.getUUID(),
                  ref:hasRef(message)
                  }
                  //Logger.log(prefills)
  let card = testError()
  return [card.build()]
}

and then the test worked.

here is the rest of my code https://github.com/william-theatlhomemaker/add-on

Thanks and cheers in advance for the help



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source