I am using strong loop for api creation. But it is giving me error. Properties of my json file are :
"properties": {
"id": {
"type": "Number"
},
"name": {
"type": "string",
"required": true
},
"language": {
"type": "string",
"required": false
},
"timezone": {
"type": "string",
"required": false
},
"labelId": {
"type": "number",
"required": false,
"default": 0
},
"street": {
"type": "string",
"required": false
},
"contact": {
"type": "number",
"required": false
},
"maincontact": {
"type": "number",
"required": false
},
"visitorTypes": {
"type": "array",
"required": false
},
"activeVisitorAvatar": {
"type": "boolean"
},
"activeLegalDocument": {
"type": "boolean"
},
"legalDocuments": {
"type": "array"
},
"logo": {
"type": "string",
"required": false
},
"logoType": {
"type": "string",
"required": false
},
"logoSmall": {
"type": "string",
"required": false
},
"logoSmallType": {
"type": "string",
"required": false
},
"activeSignOut": {
"type": "boolean"
},
"activePrint": {
"type": "boolean"
},
"activeScanTemperature": {
"type": "boolean"
},
"printerIp": {
"type": "string"
},
"activeVoicePrompt": {
"type": "boolean"
},
"mandatoryCompanyName": {
"type": "boolean"
},
"mandatoryPhoneNumber": {
"type": "boolean"
},
"sliders": {
"type": "array"
},
"slidersCount": {
"type": "number"
},
"accountId": {
"type": "number"
},
"visitorsignouttime": {
"type": "number"
},
"signoutLink": {
"type": "boolean"
},
"signoutnotification": {
"type": "boolean"
},
"deviceofflinenotification": {
"type": "boolean"
},
"deviceonlinenotification": {
"type": "boolean"
},
"emergencyAlert": {
"type": "object"
},
"autoSignOut": {
"type": "boolean"
},
"signOutTime": {
"type": "string"
},
"signOutPin": {
"type": "boolean"
},
"questionsEnabled": {
"type": "boolean"
},
"questions": {
"type": "array"
},
"logicalQuestionnaire": {
"type": "array"
},
"isEnableTemperatureCheck": {
"type": "boolean"
},
"disableTemperatureCheckScreen": {
"type": "boolean"
},
"isEnableQrCodeWithPinInside": {
"type": "boolean"
},
"isEnableComplianceAlerts": {
"type": "boolean"
},
"alertsWatchlistPriority": {
"type": "number"
},
"alertsCompliancePriority": {
"type": "number"
},
"eventNames": {
"type": "array"
},
"rooms": {
"type": "array"
},
"selfie": {
"type": "boolean"
},
"displayCompany": {
"type": "boolean"
},
"pagination": {
"type": "boolean"
}
},
My body is as follows:
let body = JSON.stringify({
id,
name,
street,
timezone: this.timezone,
activeVisitorAvatar,
activeLegalDocument,
legalDocuments,
visitorTypes,
activeSignOut,
activeScanTemperature,
activePrint,
printerIp,
labelId,
activeVoicePrompt,
mandatoryCompanyName,
mandatoryPhoneNumber,
mandatoryAnswersToQuestions,
visitorsignouttime,
signoutLink,
signoutnotification,
deviceofflinenotification,
deviceonlinenotification,
emergencyMessages: this.getEmergencyMessages(),
address: this.address,
autoSignOut,
signOutTime,
signOutPin,
questionsEnabled,
questions: this.getQuestions(),
logicalQuestionnaire: this.getLogicalQuestionnaire(),
accountId,
isEnableRememberMeForFullUIFlow,
isEnableTemperatureCheck,
isEnableQrCodeWithPinInside,
isEnableComplianceAlerts,
alertsWatchlistPriority,
alertsCompliancePriority,
selfie,
displayCompany,
temperatureThreshold: {
maximum: Number(temperatureMax),
minimum: Number(temperatureMin),
displayTextFormat: this.curr.temperatureThreshold.displayTextFormat,
},
pagination,
gdpr: {
isActive: this.curr.gdpr.isActive,
days: gdprDays,
},
purposes /*filter(purposes, (item) => item.id)*/,
isEnableIfThenQuestionnaire: isEnableIfThenQuestionnaire,
visitorQueueDisplay,
autoRefreshEntries: { enabled: autoRefreshEntries, interval: Number(autoRefreshEntriesInterval) },
visitor_email,
});
But i am receiving error {"error":{"statusCode":400,"name":"Error","message":"Value is not a string.","stack":"Error: Value is not a string.\n at Object.validate
I am stuck here ! Have tried changing the data type in json file but didn't help also can't tell which parameter is causing the error. All the values are coming from front end html and angular. My dependencies are as follows :
"loopback": "^3.0.0",
"loopback-boot": "^2.6.5",
"loopback-component-explorer": "^2.4.0",
"loopback-component-storage": "^3.0.0",
"loopback-connector-mongodb": "3.0.1",
the network request call i am making is :
try {
this.http
.post('/api/sites/edit?access_token=' + this.token, body, {
headers: contentHeaders,
})
.subscribe(
(response) => {
this.hideFlag = true;
this.toastr.success('Saved!');
this.http
.post(
'api/users/setFeaturesByAccountId',
{},
{
params: {
access_token: this.token,
listFeatures: this.listFeature,
accountId: this.curr.accountId,
},
},
)
.subscribe();
},
(error) => {
this.hideFlag = true;
this.toastr.error('Error');
this.showError = error.json().error;
console.log(error.text());
},
);
} catch (error) {
console.log(error);
}
So the error was that loopback 3 doesn't type cast correctly. I have to check which variable was not casting correct and then in my middleware parse accordingly.
if(req.body.accountId){
var numberId = parseInt(req.body.accountId);
req.body.accountId = numberId;
}
Not a perfect solution but a work around!