代码之家  ›  专栏  ›  技术社区  ›  Amit Gandole

如何通过RESTAPI从IONIC typescript读取node.js app.js中的数据?

  •  0
  • Amit Gandole  · 技术社区  · 7 年前

    我的服务是这样的:

    import { Injectable } from '@angular/core';
    import {Http, Headers} from '@angular/http';
    import 'rxjs/add/operator/map';
    
    /*
      Generated class for the PeopleSearch provider.
    
      See https://angular.io/docs/ts/latest/guide/dependency-injection.html
      for more info on providers and Angular 2 DI.
    */
    @Injectable()
    export class PeopleSearch {
      data: {name:'morr', age: 19, email:'mor@mo.com' };
      apiurl: "http://localhost:3082";
    
      constructor(public http: Http) {
        console.log('Hello PeopleSearch Provider');
    
      }
    
      load1() {
        return new Promise(resolve => { 
          let headers = new Headers();
    
          this.http.post('http://localhost:3082/users/u/',JSON.stringify(this.data),{ headers: headers })
            .map(res => res.json())
            .subscribe(data => {
              //console.log(data.user1);
    
    
              resolve(data);
            });
        });
      }  
    

    const express= require('express')
    const app= express();
    const morgan= require('morgan')
    const mysql= require('mysql')
    var cors = require('cors');
    app.use(cors());
    
    const bodyParser = require('body-parser')
    app.use(bodyParser.urlencoded({ extended: false }));
    app.use(bodyParser.json()); 
    
    
    
    const connection= mysql.createConnection({
        host: 'localhost',
        user: 'root',
        password: '',
        database: 'test'
    })
    
    app.use(morgan('combined'))
    
    
    app.use(function (req, res, next) {
        res.header("Access-Control-Allow-Origin", "*");
        res.header("Access-Control-Allow-Headers", "Cache-Control, Pragma, Origin, Authorization, Content-Type, X-Requested-With");
        res.header("Access-Control-Allow-Methods", "GET, PUT, POST");
        if ('OPTIONS' === req.method) {
            res.status(204).send();
        }
        else {
            next();
        }
    });
    app.post("/users/u", (req, res) => {
        const name=req.body.name
        const age= req.body.age
        const email= req.body.email
        const querystring="insert into users values(?,?,?,?)"
        connection.query(querystring, [11,name,age,email],(err,results,fields)=>{
            console.log("success sql post")
            res.end()        
        })
      })
    
    app.listen(3082, () => {
        console.log("Server is up and listening on 3082...")
      })
    

    1 回复  |  直到 7 年前
        1
  •  1
  •   Mathyn    7 年前

    您尚未在PeopleSearch服务中正确定义数据。仔细看看PeopleSearch课程的顶尖学生。这是:

    @Injectable()
    export class PeopleSearch {
      data: {name:'morr', age: 19, email:'mor@mo.com' };
      apiurl: "http://localhost:3082";
    

    应该是(注意“=”而不是“:”):

    @Injectable()
    export class PeopleSearch {
      data = {name:'morr', age: 19, email:'mor@mo.com' };
      apiurl = "http://localhost:3082";
    

    在类中定义属性的Typescript语法为:

    [name]:[type] = [value]
    

    在您的例子中,您定义了名称和类型,但没有定义值。实际上,您定义了特性数据并设置了类型,因此只有与您定义的特性和值完全匹配的对象才能设置为该类型。

    推荐文章