对节点服务器的Angular 2 Http调用未被调用 [英] Angular 2 Http call to Node server not being invoked

查看:56
本文介绍了对节点服务器的Angular 2 Http调用未被调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

未调用我对节点服务器的Http调用.

My Http call to the node server is not being invoked.

该呼叫从以下组件启动:

The call initiates from the following component:

@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();
  }

}

该服务的getSpecialValueproducts()被调用,它将调用节点服务器:

The service's getSpecialValueproducts() is called which calls the node server:

@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);
  }
}

与数据库的连接成功.但是以后从数据服务到node函数的调用永远不会被调用.

The connection to the database is successful. But the later call from the data service to the node function is never invoked.

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

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

module.exports = router;

const url = "mongodb://xxxxxx:xxxxxxu@xxxxx.mlab.com: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);        
    }
})

})

节点服务器:

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);
})

推荐答案

好像您缺少 subscribe .除非您订阅,否则它不会执行http请求.像这样:

Looks like you are missing the subscribe. It won't execute the http request until you subscribe. Something like this:

@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);
  }

}

这篇关于对节点服务器的Angular 2 Http调用未被调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆