Lwc: Multiple wire on getObjectInfo not firing

Created on 8 Apr 2020  路  5Comments  路  Source: salesforce/lwc

import { LightningElement, api, track, wire } from 'lwc';
import Item_OBJECT from '@salesforce/schema/Item__c';
import Brand_OBJECT from '@salesforce/schema/Seller_Brand__c';

export default class LwcInputBuyBoxItem extends LightningElement {
    @wire(getObjectInfo, { objectApiName: Item_OBJECT })
    function(data) {
        console.log(data)
    }

    @wire(getObjectInfo, {objectApiName: Brand_OBJECT})
    function(data) {
         console.log(data)
    }
}

When inspecting network requests, only the call to http://localhost:3333/api/services/data/v48.0/ui-api/object-info/Seller_Brand__c is made. If I swap the order in the js, only the item call is made. Is this a problem with wire or getObjectInfo?

Also is there a reason why sintax is not supported for these cases?

@wire(getObjectInfo, {objectApiName: Brand_OBJECT})
console.log

@wire(getObjectInfo, {objectApiName: Brand_OBJECT})
function.bind(param)

Most helpful comment

if getObjectInfo is your intended wire here, the documentation here does not specify that the wire adapter can be called imperatively (meaning that is not a supported/recommended usage)

you could go with the func1, func2 approach but be careful that you would want to track whether both wire have received data (if I understand correctly what your intention is), the tracking can be done in your wireHandler.

All 5 comments

For the first part, seems like a problem with objectApiName, which we don't maintain, cc'ing @davidturissini, he will know better.

For the second part, no, that's invalid class declaration. Keep in mind that this is not LWC specific, this is just the JS language. e.g.:

class foo {
    function.bind(param)
}
class bar {
   console.log
}

both are invalid syntax. the decorators can only be used with valid method declaration and field declarations on classes, which that syntax is neither of them.

I see, had thought it later transpiled to a direct call as a function so any function should work. Now I understand why the this was the class instance. There is no repo for the salesforce related parts to the lwc right? I try to only post the most pressing concerns to me here, but they all seem related to the sf integration.

@juvian this is not a valid issue (on the at wire) because your example is overriding the wire function (you name your wire function function, and use it on both at wires)

I am going to submit a PR with a karma test capturing this scenario and link here for your reference.

@yungcheng I see. In my use case I wanted to use the same function (which I import from another file) as a handler for both wire responses, is this the shortest way to write that?

import {wireHandler} from 'c/utils';

@wire(getObjectInfo, { objectApiName: Item_OBJECT })
func1(data) {
    wireHandler(data);
}

@wire(getObjectInfo, {objectApiName: Brand_OBJECT})
func2(data) {
    wireHandler(data);
}

Or can I skip the wire and do something like this?

 constructor() {
     super();
     getObjectInfo({objectApiName: Item_OBJECT}).then(wireHandler);
     getObjectInfo({objectApiName: Brand_OBJECT}).then(wireHandler);
 }

if getObjectInfo is your intended wire here, the documentation here does not specify that the wire adapter can be called imperatively (meaning that is not a supported/recommended usage)

you could go with the func1, func2 approach but be careful that you would want to track whether both wire have received data (if I understand correctly what your intention is), the tracking can be done in your wireHandler.

Was this page helpful?
0 / 5 - 0 ratings