· 4 years ago · Jul 25, 2021, 07:44 AM
1//Every Lapp code must have a 'main' function, that accepts following 3 parameters:
2// * queryString : contains request query string parameters as a JSON object
3// * body: contains request body as a JSON object
4// ** callback - is a function which you need to call to return response to the API caller.
5// If you fail to call this, your code will still work but request will be logged as failure.
6
7function main(queryString, body, callback) {
8 //write a simple log
9 ls.log.Info('entering Lapp...');
10 callback(body);
11
12 //pass additional JSON object to log
13 ls.log.Info('body is : ', body);
14
15 //access setting
16 let sampleSetting = ls.SETTINGS.sample_setting;
17
18 //store value in DB
19 ls.db.Put('key1', 'value', function(err, res) {
20 if (err) {
21 ls.log.Error('Error occurred while storing key in DB', err);
22 } else {
23 ls.log.Info('Key-Value stored in DB', res);
24 //now let's try to get this value back from DB
25 ls.db.Get('key1', function(err, res) {
26 if (err) {
27 ls.log.Error('Error occurred while fetching value from DB', err);
28 } else {
29 ls.log.Info('Completed Get DB response', res);
30 let fetchedValue = res.Data.Value;
31 ls.log.Info('Fetched Value from DB', fetchedValue);
32 }
33 });
34 }
35 });
36
37 //call another function. Please note that ls.db.Put and this next function will execute
38 //asynchronously i.e. anotherFunction will not wait for ls.db.Put to get completed.
39 anotherFunction(sampleSetting, function(error, data) {
40 if (error) {
41 ls.log.Error('something went wrong : ', error);
42 //return error response to API caller
43 callback(error, null);
44 } else {
45 ls.log.Info('function succeeded');
46 //return success response to API caller
47 callback(null, data);
48 }
49 });
50}
51
52function anotherFunction(param, callback) {
53 ls.log.Info('log inside anotherFunction, param : ', param);
54 //return success message with error as null
55 callback(null, 'anotherFunction succeeded');
56}