Access Salesforce Data with Lightning Web Components Open Source - Connect to Salesforce - Answers - Salesforce Trailblazer Community
Trailblazer Community
Ask Search:
Ashish KumarAshish Kumar 

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

Need Help... I am getting this type of error while solving a badge on salesforce.

User-added image

Also on adding console.log in for response

const URL = '/api/sessions';
let sessions = [];
export const getSessions = () => fetch(URL)
  .then(response => {
    console.log(response);
    if (!response.ok) {
      throw new Error('No response from server');
    }
    return response.json();
  })
  .then(result => {
    sessions = result.data;
    return sessions;
  });
export const getSession = sessionId => {
  return sessions.find(session => {
    return session.id === sessionId;
  });
}



I am getting this as response.
User-added image


api.js file is having the following codes:

// 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 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);
    }
});
const app = express();
app.use(helmet());
app.use(compression());
const HOST = process.env.API_HOST || 'localhost';
const PORT = process.env.API_PORT || 3002;
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) => {
        console.log(err);
        console.log(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`
    )
);
 
Best Answer chosen by Ashish Kumar
Suraj TripathiSuraj Tripathi
Hi Ashish,

Check this link:-
https://trailblazers.salesforce.com/answers?id=9063A000000tA8TQAU
In case you find any other issue please mention. 
If you find your Solution then mark this as the best answer. 

 

All Answers

Suraj TripathiSuraj Tripathi
Hi Ashish,

Check this link:-
https://trailblazers.salesforce.com/answers?id=9063A000000tA8TQAU
In case you find any other issue please mention. 
If you find your Solution then mark this as the best answer. 

 
This was selected as the best answer
Ashish KumarAshish Kumar
Thanks Suraj... This solution woked... we have to keep env file name like this...
 User-added image