I have created a basic local API that only deals with a post request. I am trying to implement that post request on my angular application, but it is returning some weird stuff. There is obviously something I am not doing right. I have reasons to believe that the app is reaching the API, but it's not returning what I expect. Here is my code:
1/app.component.html
import { Component } from '@angular/core';
import { HttpClient } from '@angular/common/http';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
title = 'Welcome to the parentheses balance checker';
result;
test = "hello";
constructor(private http: HttpClient) {}
onSubmit(textEnter: string) {
let json = { "string": textEnter };
console.log(json);
this.result = this.http.post('http://localhost:3000/parentheses/', json);
}
}
2/ app.components.ts
<div>
<form>
<input #textEnter placeholder="text">
<button type="submit" (click)="onSubmit(textEnter.value)">click here</button>
</form>
<pre>
{{ (result | json }}
</pre>
</div>
3/ The proxy.config.json
file:
{
"/parentheses":{
"target": "http://localhost:3000",
"secure": false,
"logLevel1": "debug"
}
}
4/ The API I am trying to reach is basically a parentheses checker:
const parenthesesChecker = require('./checker.js');
const express = require('express'), bodyParser = require('body-parser');
const router = express.Router();
router.use(bodyParser.json());
router.post('/', (req, res, next) => {
const stringCheck = ({
string: req.body
});
if (parenthesesChecker(stringCheck.string.string).result === true) {
res.status(200).json({
"isValid": true
});
} else {
res.status(400).json({
"isValid": false,
"errorDetails": {
"preceedingParentheses": `${parenthesesChecker(stringCheck.string.string).preceeding}`,
"erroneosParentheses": `${parenthesesChecker(stringCheck.string.string).erroneos}`
}
});
};
});
module.exports = router;
5/ Javascript function it is referring to:
module.exports = function (string) {
const openArray = ["(", "[", "{", "<"];
const closeArray = [")", "]", "}", ">"];
let i = 0, parOpen = 0, openingPar= "", closingPar = [], erroneos = "";
for(i; i < string.length; i++) {
if (openArray.includes(string[i])) {
openingPar += string[i];
parOpen++;
closingPar.push(closeArray[openArray.indexOf(string[i])]);
}
if (closeArray.includes(string[i])) {
let erroneos = string[i];
parOpen--;
if (string[i] === closingPar[closingPar.length - 1]) {
closingPar.pop();
} else {
return { result: false, preceeding: openingPar, erroneos: erroneos };
};
};
if (parOpen < 0) return { result: false, preceeding: openingPar, erroneos: erroneos };
}
return { result: (parOpen === 0 ? true : false), preceeding: openingPar, erroneos: erroneos };
};
My problem: I get in return something like that, and it only displays a fraction of a second:
{
*_isScalar*: false,
*source*: {
*_isScalar*: false,
*source*: {
etc..
I don't know how to interpret that.