Altough this code works, from my local, as sson as I deploy it to Edge, it causes me issue,
Can somone help.
Thanks
Package.json
{
"name": "baas",
"version": "1.0.0",
"description": "",
"main": "server.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": {
"name": "XXX",
"email": "xxxx@xxxxx.com"
},
"dependencies": {
"body-parser": "^1.18.2",
"cors": "^2.8.4",
"express": "^4.16.2",
"usergrid": "^2.0.0-rc.2"
}
}
Code
var express = require('express');
var bodyParser = require('body-parser')
var UsergridClient = require('./node_modules/usergrid/lib/client')
var UsergridQuery = require('./node_modules/usergrid/lib/query')
// var cors =require('cors')
/*Set up Express environment and enable it to read and write JavaScript.*/
var app = express()
// app.all(cors())
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({
extended: true
}))
var config = require('./config');
// Initialize Usergrid
var Usergrid = new UsergridClient({
'orgId' : config.organization,
'appId' : config.application,
'clientId' : config.clientId,
'clientSecret' : config.clientSecret,
"authMode": "UsergridAuth.AUTH_CLIENT_ID",
"baseUrl": "https://baas-ug002sr.apigee.net",
Url: "https://baas-address.apigee.net",
logging : config.logging
});
/*Authenticate user*/
Usergrid.authenticateApp(function(err, usergridResponse) {
if (usergridResponse.ok) {
console.log('app is now authenticated')
}
})
// GET /
var rootTemplate = {
'employees' : {
'href' : './employees'
}
};
app.get('/', function(req, resp) {
resp.jsonp(rootTemplate);
});
// GET /profiles
app.get('/profiles', function(req, res) {
getProfiles(req, res);
});
function getProfiles(req, res) {
Usergrid.GET('/profile', function(error, usergridResponse) {
if (error) {
res.jsonp(500, {
'error' : JSON.stringify(error)
});
return;
}
var entities = usergridResponse.entities;
//console.log('entities: ' + JSON.stringify(entities, null, 2));
var emps = entities.map( (emp) => { return { entity: emp.accountA }});
res.jsonp(emps);
});
}
app.post('/profile', function(req, res) {
if (!req.is('json')) {
res.jsonp(400, {
error : 'Bad request'
});
return;
}
var payload = req.body;
var entity = {
type: 'restaurant',
restaurant: payload.restaurant,
cuisine: payload.cuisine
}
if ((entity.restaurant === undefined) || ( entity.cuisine === undefined) ) {
res.jsonp(400, {
error : 'Bad request'
});
return;
}
Usergrid.POST(entity, function(error, usergridResponse, entity) {
// entity should now have a uuid property and be created
res.send(201);
});
});
// Listen for requests until the server is stopped
app.listen(process.env.PORT || 9000);
console.log('The server is running!');
Error
{
"fault": {
"faultstring": "Script node executed prematurely: syntax error\nsyntax error\n at module.js:439\n at module.js:474\n at module.js:356\n at module.js:312\n at module.js:497\n at startup (trireme.js:142)\n at trireme.js:923\n",
"detail": {
"errorcode": "scripts.node.runtime.ScriptExitedError"
}
}
}