0

In the below code i have created object "cart" and i have assigned that "cart" object to "addToCart" variable but not able to see the assigned values of "cart" and "addToCart" variable in addItemToCart() method so please tell me how to create object and assign values to it

And "let span = this.template.querySelectorAll('span');" has following values

span[0].innerText  // a005h00000nCm1uAAC
span[1].innerText  // Sticky Baked Chicken Wings
span[2].innerText  // 3 it is Number

itemDetails.js

import { LightningElement,api,wire } from 'lwc';
import { subscribe,MessageContext } from 'lightning/messageService';
import FOODITEM from '@salesforce/messageChannel/FoodItem__c'
export default class itemDetails extends LightningElement {
    
    Id;
    Name;
    Quantity = "3";
    subscription = null;

    addToCart ;

    @wire(MessageContext)
    messageContext
    
    connectedCallback(){
        this.subscribeToMessageChannel(); 
    }

    subscribeToMessageChannel(){
        this.subscription = subscribe(
            this.messageContext,
            FOODITEM,
            (message) => this.handleMessage(message)
            )
    }
    handleMessage(message){
        let values = JSON.parse(JSON.stringify(message.dishName));
        this.Id = values.Id;
        this.Name = values.Name;
    }
    addItemToCart(){
    
        let span  = this.template.querySelectorAll('span');

        let cart  = {
                i : span[0].innerText,
                n : span[1].innerText,
                q : span[2].innerText
        }
    this.addToCart = cart;
    console.log("cart:-",cart);
    console.log("cart:-",this.addToCart);
    }
}

itemDetails.html

<template>
    <div>
        <lightning-formatted-rich-text></lightning-formatted-rich-text>
        <p>Id: <span>{Id}</span></p>
        <p>Name of the dish: <span>{Name}</span></p>
        <lightning-layout class="slds-m-top_x-small">
            <lightning-button title="add" icon-name="utility:add"></lightning-button>
            <p>Quantity <span>{Quantity}</span></p>
            <lightning-button title="remove" icon-name="utility:dash"></lightning-button>
        </lightning-layout>
        <lightning-button label="Add To Cart" onclick={addItemToCart}></lightning-button>
    </div>
</template>
3
  • What do you see if you use console.log("cart:-",JSON.stringify(cart));?
    – Keith C
    Mar 19 at 20:57
  • if i use console.log(cart) it shows proxy(object) when it is empty but when i put some value in it It doesn't execute and type of empty object shows object Mar 20 at 9:58
  • 1
    That is why you need to use e.g. JSON.stringify so the proxy is converted to actual data.
    – Keith C
    Mar 20 at 11:03

1 Answer 1

0

Given you already have the data available within your component, I would avoid trying to get this data using a query selector and use it directly.

To construct the cart you should be able to do the following:

addItemToCart(){

    let cart  = {
            i : this.Id
            n : this.Name
            q : this.Quantity
    }
    
    this.addToCart = cart;
}
1
  • i also did the same but it is also not working Mar 20 at 9:51

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .