【问题标题】:Angular 2 Http call to Node server not being invoked未调用对节点服务器的 Angular 2 Http 调用
【发布时间】:2017-10-20 03:46:43
【问题描述】:

我对节点服务器的 Http 调用没有被调用。

调用从以下组件发起:

@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() 被调用,它调用节点服务器:

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

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

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

【问题讨论】:

  • 你能显示你在你的应用程序上安装路由器的位置吗?
  • 我不清楚你的问题。
  • 我已经包含了节点服务器,如果这可以阐明您的问题。

标签: node.js angular


【解决方案1】:

您似乎缺少subscribe。在您订阅之前,它不会执行 http 请求。像这样的:

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

}

【讨论】:

    猜你喜欢
    • 2018-02-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-02
    • 2013-10-31
    • 2017-02-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多