Access Salesforce Data with Lightning Web Components Open Source - Connect to Salesforce - Answers - Salesforce Trailblazer Community
Trailblazer Community
Ask Search:
Dawn KunigDawn Kunig 

Access Salesforce Data with Lightning Web Components Open Source - Connect to Salesforce

I'm working through this project and can't figure out how to get the connection to Salesforce to work. I've copied all of the code into my project and even restarted and it's still not working. I get the error: 
Unhandled Rejection (Error): No response from server
(anonymous function)
./src/client/modules/data/sessionService/sessionService.js:5
  2 | let sessions = [];
  3 | export const getSessions = () => fetch(URL).then(response => {
  4 |   if (!response.ok) {
> 5 |     throw new Error('No response from server');
  6 |   }
  7 | 
  8 |   return response.json();

and the challenge failes with "We couldn't find Other Apex API in the login history."
Best Answer chosen by Dawn Kunig
Dawn KunigDawn Kunig
I tried this again today. All I did was reinstall the jsforce and dotenv packages (npm install jsforce dotenv). I didn't change anything else. When I ran watch, I immediately noticed some differences in the terminal output and then the connection was successful and I passed the module.

All Answers

Tommaso BolisTommaso Bolis
Were you able to verify the application on http://0.0.0.0:3001/api/sessions?
Dawn KunigDawn Kunig
Nope. I get Cannot GET /api/sessions.
Kaustubh (KL) LabheKaustubh (KL) Labhe
were you able to figure this out? 
Im getting the same issue

I guess the problem is in server/index.js file on line 4
const { SF_USERNAME, SF_PASSWORD, SF_TOKEN, SF_LOGIN_URL } = process.env;
Dawn KunigDawn Kunig
I haven't. I was able to pass the following module in the trail by removing a "this" in a "this.session" line, but this one I can't figure out. i'm assuming it's a glitch in the instructions because this trail/module is so new...but I can't complete the open quest because of it!
Kaustubh (KL) LabheKaustubh (KL) Labhe
Dawn,

I have asked a question re this on SF Stackexchange if you wanna follow: https://salesforce.stackexchange.com/questions/281182/lwc-os-trailhead-connect-to-salesforce

Cheers
Dawn KunigDawn Kunig
I tried this again today. All I did was reinstall the jsforce and dotenv packages (npm install jsforce dotenv). I didn't change anything else. When I ran watch, I immediately noticed some differences in the terminal output and then the connection was successful and I passed the module.
This was selected as the best answer
Tommaso BolisTommaso Bolis
Thank you for letting us know.
Auroiah MorganAuroiah Morgan
I am getting the same issue even after reinstalling the npm install jforce dotenv:
[HPM] Error occurred while trying to proxy request /api/sessions from localhost:3001 to http://localhost:3002 (ECONNREFUSED) (https://nodejs.org/api/errors.html#errors_common_system_errors)

 
Stuart McKitrickStuart McKitrick
The issue that I had on this was that my .env file saved as .env.txt. So, the system was reading it as a text file instead as an env file. Once I renamed it to remove the .txt, everything worked.
Kaustubh (KL) LabheKaustubh (KL) Labhe
Thanks @Dawn, I will try reinstalling this like you suggested
Arek JarzabekArek Jarzabek
I was struggling with the same issue, I noticed that while trying to start npm run watch the app is crashing with this error:
Error: listen EADDRINUSE: address already in use :::3002

The solution was to kill the old node processes that were blocking the port - after they were killed, the Express server started correctly and communications to SF was established without problems, thus letting me pass the verification. Here you can find further explanation and ways to kill the processes correctly on Unix or Win:
https://stackoverflow.com/questions/4075287/node-express-eaddrinuse-address-already-in-use-kill-server

Hope that helps!
Tommaso BolisTommaso Bolis
Thank you Arkadiusz.
Lorenzo Boy-Touma AibaLorenzo Boy-Touma Aiba
I passed the module, but I still have the same error. Did someone fix this? 
Brook GongBrook Gong
api/sessionsI passed the module and no error displayed when open browser on http://0.0.0.0:3001/api/sessions.

I think the following points we should pay attention:
1. Make sure the name of file .env is correct
User-added image
values for 
SF_USERNAME
SF_PASSWORD
SF_TOKEN
must be correct. 
No need to add quotation marks on the values, for example, we only need assignemnt like this
SF_USERNAME = tester.newzealand@resourceful-badger-aswere.com
We can manually test values of 'SF_USERNAME' and 'SF_PASSWORD' can login 'SF_LOGIN_URL=https://login.salesforce.com',
Better to reset security token and we can follow steps from https://trailhead.salesforce.com/content/learn/projects/access-salesforce-data-with-lightning-web-components-open-source/create-a-salesforce-environment

2. I didn't follow the session 'Update Data Service with a New Endpoint' in Trailhead to update 'const URL' value to '/api/sessions'. I'm still using 
const URL = 'http://conference-lwc-app.herokuapp.com/api/sessions';
Mohamed MrabetMohamed Mrabet

I was facing the same issue but was able to quickly solve it by simply (1) Killing the server, and (2) running npm run watch one more time.

Here is how I killed the server :
On my VScode Terminal, I typed in the following command : taskkill /F /IM node.exe

Kudos to Arkadiusz Jarzabek who set me on the right track. 

Yassine NachtiYassine Nachti
I had the same issue. what I found out is that three things one can run into.
1. The .env file has to be in the correct name format vscode will give it a .txt extension so rename it to delete the .txt
2. When you reset the token for some reason it does it twice and you get two emails use the token in the last email you get. 
3. Most importantly, if you are having issues with connection and are making changes to the .env file you want to make sure you are running the app. So use ctrl+C and enter Y for yes. make your changes to the .env file. CLOSE Vscode completely and then do your npm run watch. 
Hope this helps someone.  
Pascal TaghjiPascal Taghji
Hi,

Please can someone share the index.js file ?

As this file was missing from the src|server folder (I just found an api.js file in this folder), I created one based on the one I found here on Github :
https://github.com/rodriguezandrade/Salesforce-Conference-App/blob/master/src/server/index.js
Mykhailo SavchenkoMykhailo Savchenko

Pascal Taghji you can modify api.js file.

here what I've got 

// Simple Express server setup to serve for local testing/dev API server
const compression = require('compression');
const helmet = require('helmet');
const express = require('express');

const app = express();
app.use(helmet());
app.use(compression());

const HOST = process.env.API_HOST || 'localhost';
const PORT = process.env.API_PORT || 3002;

const jsforce = require('jsforce');
require('dotenv').config();
const { SF_USERNAME, SF_PASSWORD, SF_TOKEN, SF_LOGIN_URL } = process.env;
if (!(SF_USERNAME && SF_PASSWORD && SF_TOKEN && SF_LOGIN_URL)) {
    console.error(
        'Cannot start app: missing mandatory configuration. Check your .env file.'
    );
    process.exit(-1);
}
const conn = new jsforce.Connection({
    loginUrl: SF_LOGIN_URL
});
conn.login(SF_USERNAME, SF_PASSWORD + SF_TOKEN, err => {
    if (err) {
        console.error(err);
        process.exit(-1);
    }
});

app.get('/api/sessions', (req, res) => {
    const soql = `SELECT Id, Name, toLabel(Room__c), Description__c, format(Date_and_Time__c) formattedDateTime,
        (SELECT Speaker__r.Id, Speaker__r.Name, Speaker__r.Description, Speaker__r.Email, Speaker__r.Picture_URL__c FROM Session_Speakers__r)
        FROM Session__c ORDER BY Date_and_Time__c LIMIT 100`;
    
        conn.query(soql, (err, result) => {
            if (err) {
                res.sendStatus(500);
            } else if (result.records.length === 0) {
                res.status(404).send('Session not found.');
            } else {
                const formattedData = result.records.map(sessionRecord => {
                    let speakers = [];
                    if(sessionRecord.Session_Speakers__r){
                        speakers = sessionRecord.Session_Speakers__r.records.map(
                            record => {
                                return {
                                    id: record.Speaker__r.Id,
                                    name: record.Speaker__r.Name,
                                    email: record.Speaker__r.Email,
                                    bio: record.Speaker__r.Description,
                                    pictureUrl: record.Speaker__r.Picture_URL__c
                                };
                            }
                        );
                    }
                    return {
                        id: sessionRecord.Id,
                        name: sessionRecord.Name,
                        dateTime: sessionRecord.formattedDateTime,
                        room: sessionRecord.Room__c,
                        description: sessionRecord.Description__c,
                        speakers
                    };
                });
                res.send({ data: formattedData });
            }
        });
});

app.listen(PORT, () =>
    console.log(
        `✅  API Server started: http://${HOST}:${PORT}/api/v1/endpoint`
    )
);
Abhipsha MohapatraAbhipsha Mohapatra
@Mykhailo Savchenko : The above answer is helped clear the trailhead. Thanks a lot.
Pascal TaghjiPascal Taghji
Hi Mykhailo Savchenko,
Thanks a lot for your help !
It worked for me too.
Trailhead removed the reference to index.js file in the description.
Shashi MogalgiddiShashi Mogalgiddi
Hello All:

This exercise is so weird that I was getting the error even after following the same steps over and over again.  Followed the steps as metioned by Dawn Kunig and was still getting the error.  Even now I re-created the .env file, ran "npm jsforce dotenv" command and still got the error.  Ran "npm run watch" multiple time and finally pass the exercise but still see the error in the terminal on visual studio code.  Weird....
 
Kamal Thapa MagarKamal Thapa Magar
I solved the problem by moving the .env file just above the .eslingtingnore I mean to say that .env file should be outside the src file

User-added image