我正在尝试在Angular前端中显示数据,该数据是针对链接到Node后端的Oracle DB调用的。当前,如果我转到phsyiscal API链接,我将看到数据并将其显示在我的后端console.log中。
我在这里错过了一些简单的事情吗?我是新手,如果这很容易,请提前道歉。
角度页面组件
import { Component, OnInit } from '@angular/core';
import { Observable } from 'rxjs';
import { DBService, Group} from '../db.service';
import { HttpClient } from '@angular/common/http';
import { NgIf } from '@angular/common/src/directives/ng_if';
import { element } from 'protractor';
import { ActivatedRoute } from '@angular/router';
import { Router } from '@angular/router';
@Component({
selector: 'app-flow',
templateUrl: './flow.component.html',
styleUrls: ['../css.css']
})
export class FlowComponent implements OnInit {
groups: Group[];
constructor(
private auth: AuthenticationService,
private dbService: DBService,
private route: ActivatedRoute,
private router: Router
) {}
ngOnInit() {
this.dbService.getGroup2().subscribe(groups => this.groups = groups);
}
}
数据库服务代码
getGroup2(): Observable<Group[]> {
const url = 'http://localhost:4200/api/' + 'employees';
const data = ({
});
return this._http.post(url, data)
.pipe(
map((res) => {
console.log(res);
return <Group[]> res;
})
);
}
后端
services / router.js
const express = require('express');
const router = new express.Router();
const employees = require('../controllers/employees.js');
router.route('/employees')
.get(employees.get);
module.exports = router;
services / database.js
const oracledb = require('oracledb');
const dbConfig = require('../config/database.js');
async function initialize() {
const pool = await oracledb.createPool(dbConfig.hrPool);
}
module.exports.initialize = initialize;
// close the function
async function close() {
await oracledb.getPool().close();
}
module.exports.close = close;
// simple executre function
function simpleExecute(statement, binds = [], opts = {}) {
return new Promise(async (resolve, reject) => {
let conn;
opts.outFormat = oracledb.OBJECT;
opts.autoCommit = true;
try {
conn = await oracledb.getConnection();
const result = await conn.execute(statement, binds, opts);
resolve(result);
} catch (err) {
reject(err);
} finally {
if (conn) { // conn assignment worked, need to close
try {
await conn.close();
} catch (err) {
console.log(err);
}
}
}
});
}
module.exports.simpleExecute = simpleExecute;
controllers / employees.js
const employees = require('../db_apis/employees.js');
async function get(req, res, next) {
try {
const context = {};
// context.id = parseInt(req.params.id, 10);
const rows = await employees.find(11);
if (req.params.id) {
if (rows.length === 1) {
res.status(200).json(rows[0]);
} else {
res.status(404).end();
}
} else {
res.status(200).json(rows);
}
} catch (err) {
next(err);
}
}
module.exports.get = get;
db_apis / employees.js
const database = require('../services/database.js');
const baseQuery =
`SELECT * FROM DB.EMPLOYEES`;
async function find(context) {
// let query = baseQuery;
// const binds = {};
// if (context.id) {
// binds.employee_id = context.id;
// query += `\nwhere employee_id = :employee_id`;
// }
const result = await database.simpleExecute(baseQuery);
console.log(result.rows);
return result.rows;
}
module.exports.find = find;
答案 0 :(得分:1)
您有router.route('/employees').get(employees.get);
,但在服务中您正在this._http.post(url, data)
您应该执行get
请求this._http.get(url)...
。
答案 1 :(得分:1)
在您的getGroup2中,标头部分在哪里?
getGroup2(): Observable<Group[]> {
const url = 'http://localhost:4200/api/' + 'employees';
const data = ({
});
return this._http.post(url, data)
.pipe(
map((res) => {
console.log(res);
return <Group[]> res;
})
);
}
const headers = new HttpHeaders({
'Content-Type': 'application/json',
'Authorization': `Bearer fs4df4sdfsd4fs`
});
const options = {headers: _headers}
return this._http.post(url, data, options)
希望这会有所帮助