Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Google IOT Core get device Nodejs client getClient initialization return value problem #1579

Closed
radianceltd opened this issue Jan 3, 2020 · 8 comments
Assignees
Labels
type: question Request for information or clarification. Not an issue.

Comments

@radianceltd
Copy link

radianceltd commented Jan 3, 2020

Hello, I have configured the service credential JSON, and import it under the project main file. The data read by this API is always empty. I do n’t know where I wrote the problem. What I want to achieve is through the new Google v1-client.projects.locations.registries.devices.list to get the API, but one encountered this problem, the following is my code:

const getClient = (res) => {
    // the getClient method looks for the GCLOUD_PROJECT and GOOGLE_APPLICATION_CREDENTIALS
    // environment variables if serviceAccountJson is not passed in
    const authClient =  google.auth.getClient({
        keyFilename: 'riltmw025tool-2019-296a54b9be.json',
        scopes: ['https://www.googleapis.com/auth/cloud-platform'],
    });

    const discoveryUrl = `${DISCOVERY_API}?version=${API_VERSION}`;

    google.options({
        auth: authClient,
    });

    try {
        res.send(google.discoverAPI(discoveryUrl));
        return google.discoverAPI(discoveryUrl);
    } catch (err) {
        console.error('Error during API discovery.', err);
    }
};

//List all devices in the specified registry
const registry_list_Device = async (
    client,
    registryId,
    projectId,
    cloudRegion,
    res
) => {
    const parentName = `projects/${projectId}/locations/${cloudRegion}`;
    const registryName = `${parentName}/registries/${registryId}`;

    const request = {
        parent: registryName,
    };
    res.send(client);
    try {

        const {data} = await client.projects.locations.registries.devices.list(
            request
        );

        res.status(200).send(data);
        console.log('Current devices in registry:', data['devices']);
    } catch (err) {
        res.status(402).send(err);
        console.error('Could not list devices', err);
    }
}
@radianceltd radianceltd changed the title Google IOT Core获取设备Nodejs客户端getClient初始化返回值问题 Google IOT Core get device Nodejs client getClient initialization return value problem Jan 3, 2020
@radianceltd
Copy link
Author

When I read the client API, Getclient returned to me like this, I don't know how to call const {data} = await client.projects.locations.registries.devices.list (request); the data data
image

@yoshi-automation yoshi-automation added triage me I really want to be triaged. 🚨 This issue needs some love. labels Jan 4, 2020
@fhinkel
Copy link
Contributor

fhinkel commented Jan 8, 2020

@hongalex Could you have a look at this IoT issue? Thanks

@fhinkel fhinkel added type: question Request for information or clarification. Not an issue. and removed 🚨 This issue needs some love. triage me I really want to be triaged. labels Jan 8, 2020
@radianceltd
Copy link
Author

@hongalex Can you help me see my problem

@hongalex
Copy link
Contributor

hongalex commented Jan 9, 2020

I think the main issue here is that google.discoverAPI(discoveryUrl) is an async call that returns a Promise rather than a value. Therefore, your res.send call doesn't actually send the actual client. Try using await google.discoverAPI(discoveryUrl) in your getClient function. This means that getClient will have to marked async as well.

Since this isn't an issue with the samples themselves, I'm marking this issue as closed. If you continue to experience issues, I recommend bringing your question to one of the forums described here: https://cloud.google.com/support/docs/community

Many members of the GCP community---including Google employees and other experts---help out on those forums.

@radianceltd
Copy link
Author

@hongalex Thank you for your answer. Like you described, google.discoverAPI (discoveryUrl) returns an object. Promise, I did n’t understand what this data is, but I passed const client = getClient ('riltmw025tool-2019-21396a54b9be.json'); passed it to the Google API I need to call, such as: listDevicesForGateway, then this client value Passed to listDevicesForGateway as the client, always accessing client.projects.locations.registries.devices.list returns no data

@radianceltd
Copy link
Author

radianceltd commented Jan 10, 2020

exports.ymqtthelloa = async (req,res) => {

    const cloudRegion = 'us-central1';
    const gatewayId = 'my-region';
    const projectId = 'riltmw025tool';
    const registryId = 'my-registry-006s';

    const client = getClient('riltmw025tool-2019-21396a54b9be.json');
    listDevicesForGateway(client, projectId, cloudRegion, registryId, gatewayId);
    console.log("异步读取lo: " + client);
}

const getClient = async (serviceAccountJson,res) => {
    // the getClient method looks for the GCLOUD_PROJECT and GOOGLE_APPLICATION_CREDENTIALS
    // environment variables if serviceAccountJson is not passed in
    const authClient = await google.auth.getClient({
        keyFilename: serviceAccountJson,
        scopes: ['https://www.googleapis.com/auth/cloud-platform'],
    });
   // console.log("11"+authClient.toString());
    const discoveryUrl = 'https://cloudiot.googleapis.com/v1/projects/riltmw025tool-2019/locations/us-central1/registries/my-registry/devices';
   // console.log("22"+discoveryUrl.toString());
    google.options({
        auth: authClient,
    });

    try {
        console.log("33"+google.discoverAPI(discoveryUrl));
        return google.discoverAPI(discoveryUrl);
    } catch (err) {
        console.log("402"+err);
        console.error('Error during API discovery.', err);
    }
};

const listDevicesForGateway = async (
    client,
    projectId,
    cloudRegion,
    registryId,
    gatewayId
) => {
    const parentName = `projects/${projectId}/locations/${cloudRegion}/registries/${registryId}`;
    const request = {
        parent: parentName,
        'gatewayListOptions.associationsGatewayId': gatewayId,
    };

    let devices;
    try {
        const {data} = await client.projects.locations.registries.devices.list(
            request
        );
        devices = data.devices;
    } catch (err) {
        console.error('Could not list devices', err);
        return;
    }

    console.log('Current devices bound to gateway: ', gatewayId);
    if (devices && devices.length > 0) {
        devices.forEach(device => {
            console.log(`\tDevice: ${device.numId} : ${device.id}`);
        });
    } else {
        console.log('No devices bound to this gateway.');
    }
    // [END iot_list_devices_for_gateway]
};

Why does client.projects.locations.registries.devices.list not return data?

@hongalex
Copy link
Contributor

As mentioned earlier, this is not the proper place to raise this question. The issue tracker for this repository is only for sample related issues.

If you require assistance debugging your code, please post your question on StackOverflow and the community there (including myself) should be better able to help you.

Thanks!

@radianceltd
Copy link
Author

@hongalex Okay, thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: question Request for information or clarification. Not an issue.
Projects
None yet
Development

No branches or pull requests

4 participants