2017-07-27 1 views
0

Mein Http-Aufruf an den Node-Server wird nicht aufgerufen.Angular 2 Http-Aufruf an Node-Server wird nicht aufgerufen

Der Aufruf initiiert von der folgenden Komponente:

@Component({ 
    selector: 'app-special-value', 
    templateUrl: './special-value.component.html', 
    styleUrls: ['./special-value.component.css'] 
}) 

export class SpecialValueComponent implements OnInit { 

    constructor(private dataService: DataService) { } 

    ngOnInit() { 
    this.getSpecialValueProducts(); 
    } 

    getSpecialValueProducts() { 
    this.dataService.getSpecialValueProducts(); 
    } 

} 

Der getSpecialValueproducts Dienst() aufgerufen wird, die den Knoten Server ruft:

@Injectable() 
export class DataService { 
private specialValueUrl = "/api/product/specialvalue"; 

    constructor(private http: Http) { } 

    getSpecialValueProducts() { 
    console.log('getSpecialValueProducts() called'); 

    return this.http.get(this.specialValueUrl) 
     .map((response: Response) => response.json()) 
     .catch(this.handleError); 
    } 
} 

Die Verbindung zur Datenbank erfolgreich ist. Der spätere Aufruf vom Datendienst an die Knotenfunktion wird jedoch nie aufgerufen.

const express = require('express'); 
const router = express.Router(); 
const mongoose = require('mongoose'); 

const Product = require('../models/product'); 

module.exports = router; 

const url = "mongodb://xxxxxx:[email protected]:111111/xxxxxx"; 

mongoose.Promise = global.Promise; 
mongoose.createConnection(url, function(err) { 
    if(err) { 
     console.log('Error!!!' + err); 
    } else { 
     console.log('Connected to Database!'); 
    } 
}); 

router.get('/product/specialvalue', function(req, res) { 
    console.log('Get specialvalue called '); 

Product.find({'specialValue': true}) 
.sort({'price': 1}) 
.exec(function(err, products) { 
    if(err) { 
     console.error('Error retrieving special value products!'); 
    } else { 
     console.log("products = " + JSON.stringify(products)); 
     res.json(products);   
    } 
}) 

}) 

Node Server:

const express = require('express'); 
const bodyParser = require('body-parser'); 
const path = require('path'); 

const api = require('./server/routes/api'); 
const port = 4200; 

const app = express(); 
app.use(express.static(path.join(__dirname, 'dist'))); 

app.use(bodyParser.urlencoded({extended: true})); 
app.use(bodyParser.json()); 

app.use('/api', api); 

app.get('*', (req, res) => { 
    res.sendFile(path.join(__dirname, 'dist/index.html')); 
}); 

app.listen(port, function() { 
    console.log('@@@@ nglowes01 Server running on localhost: ' + port); 
}) 
+0

Können Sie zeigen, wo Sie den Router auf Ihrer App montieren? – Paul

+0

Ich bin nicht klar auf Ihre Frage. – koque

+0

Ich habe den Knoten-Server aufgenommen, wenn das Ihre Frage beleuchten würde. – koque

Antwort

0

Sieht aus wie Sie die subscribe fehlen. Die HTTP-Anfrage wird erst ausgeführt, wenn Sie sich angemeldet haben. So etwas wie das:

@Component({ 
    selector: 'app-special-value', 
    templateUrl: './special-value.component.html', 
    styleUrls: ['./special-value.component.css'] 
}) 

export class SpecialValueComponent implements OnInit { 
    specialValue; 

    constructor(private dataService: DataService) { } 

    ngOnInit() { 
    this.getSpecialValueProducts(); 
    } 

    getSpecialValueProducts() { 
    this.dataService.getSpecialValueProducts().subscribe(value => this.specialValue = value); 
    } 

} 
Verwandte Themen