承诺总是在Angular 4中返回“undefined”

时间:2017-09-21 01:33:33

标签: angular typescript promise

我正在尝试使用http.get从JSON传递到其他组件,但总是得到“未定义”

在我的service.ts中,我有getDate函数

getData(): Promise<MinnanoNihongo[]>{
   return this.http.get("../../assets/json/leccion1.json")
          .toPromise()
          .then(response => response.json().data as MinnanoNihongo[]);
}

在我的组件中,我打电话使用:

this.MinnaData
         .getData()
         .then( data => this._Minna = data );
    console.log(this._Minna);  //show Undefined in console

我是使用Angular和打字稿的新手,所以我不知道是什么产生了这个问题。

2 个答案:

答案 0 :(得分:1)

这是因为console.log(this._Minna);.then( data => this._Minna = data );

之前执行

尝试

this.MinnaData
         .getData()
         .then( data => this._Minna = data ).then(() => {
             console.log(this._Minna)
         });

答案 1 :(得分:0)

你应该在那里打印console.log

from py2neo import Graph,Node,Relationship
test_graph = Graph(
    "http://localhost:7474", 
    username="neo4j", 
    password="******"
)
test_node_1 = Node(label = "Person",name = "test_node_1")
test_node_2 = Node(label = "Person",name = "test_node_2")
test_graph.create(test_node_1)
test_graph.create(test_node_2)

node_1_call_node_2 = Relationship(test_node_1,'CALL',test_node_2)
node_2_call_node_1 = Relationship(test_node_2,'CALL',test_node_1)
test_graph.create(node_1_call_node_2)
test_graph.create(node_2_call_node_1)

find_code_1 = test_graph.find_one(
  label="Person",
  property_key="name",
  property_value="test_node_1"
)
print (find_code_1)