0

I try to write something to the database soql in JS, but it not works. When I click to the button 'Add to Cart' then is called handlePlaceOrder(). Here is called Apex method placeOrder('hello') with parameter String. And then it crashed "an internal server error". When I executed only Apex method placeOrder('Order01') in Developer Console, it works, writes nameOfOrder to the database.

Here are codes:

html (OK):

<template>
  <div class="shopping-cart">
    <template if:true={messageFromMain}>
      <div class="container">
        <h1>Items in Cart</h1>
        <lightning-card title="Shopping cart">
          <lightning-button 
            class="slds-m-left_x-small"  
            label="Place order" 
            title="Place order"
            variant="success"
            onclick={handlePlaceOrder}>
          </lightning-button>
        </lightning-card>
      </div>
    </template>
    <template if:false={messageFromMain}>
      <h1>Select the shoes</h1>
    </template>
  </div>
</template>

JS (bug):

import { LightningElement, api, wire } from 'lwc';
import placeOrder from '@salesforce/apex/ProductMaster.placeOrder';

export default class ShoppingCart extends LightningElement {
  nameOfOrder;

  @api messageFromMain;

  @wire(placeOrder, { nameOfOrder: '$nameOfOrder'})


  // handle click to button Place order
  handlePlaceOrder() {
    console.log('It was pressed button \'Add to Cart\'.');

    placeOrder('hello');
  }
}

Apex (OK):

@AuraEnabled(cacheable=true)
public static void placeOrder(String nameOfOrder) {
  Shop_Order__c newOrder = new Shop_Order__c(
    Text__c = nameOfOrder
  );
  insert newOrder;
}

Error:

Error: An internal server error has occurred
Error ID: 185265246-97831 (-1502885840)
    at U.B.mk (https://static.lightning.force.com/eu40/auraFW/javascript/Q8onN6EmJyGRC51_NSPc2A/aura_prod.js:656:438)

What is not correct on this JavaScript code? Here is an example with similar problem.

1 Answer 1

1
@wire(placeOrder, { nameOfOrder: '$nameOfOrder'})

You might have to delete that line. Your handlePlaceOrder() is treated as continuation of that line (ignore newlines), as a handler what to do (callback) when the asynchonous processing finishes. In the answeryou linked compare what you wrote with "fetchedContact"

And in handlePlaceOrder I think you need to pass params as JSON object where names match the names of apex parameters. Try

Apex

@AuraEnabled
public static Id insertAccount(String n){
    Account a = new Account(Name = n);
    insert a;
    return a.Id;
}

Component's relevant html

<lightning-button label="test" onclick={handlePlaceOrder}>
</lightning-button>

Component's relevant JS

import insertAccount from '@salesforce/apex/SomeClass.insertAccount';
export default class SomeComp extends LightningElement {
handlePlaceOrder() {
        console.log('It was pressed button \'Add to Cart\'.');
        insertAccount({ n: 'hello' })
            .then(result => {
                console.log(result);
            })
            .catch(error => {
                debugger;
            });
    }

}

Browser's console enter image description here

And it saved the name OK

enter image description here

Sign up to request clarification or add additional context in comments.

4 Comments

I tried. Now, it's without errors, but nothing is written in database. 'hello' is not in Shop_Order__c. Where is the 'hello' lost? if I understand correctly, fetchedContact is for get values, I would like set values.
It's just a string (and not say a helper class object) and you passed the parameter exactly as the name of apex parameter? Works for me, check the extended example.
It was error in Apex. It doesn't work with (cacheable=true). Correct Apex code: @AuraEnabled public static Id placeOrder(String nameOfOrder) { Shop_Order__c newOrder = new Shop_Order__c(Text__c = nameOfOrder); insert newOrder; return newOrder.Id; } Wrong Apex code: @AuraEnabled(cacheable=true) public static Id placeOrder(String nameOfOrder) { Shop_Order__c newOrder = new Shop_Order__c(Text__c = nameOfOrder); insert newOrder; return newOrder.Id; } Thanks. @eyescream solved it.
I don't understand this stackoverflow. Code formatting in comments is not correct. The code in my previous post is so illegible.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.