我需要编写一些对象数组和接口数组来使用Angular app来完成我的项目,但是我必须面对那里的值的错误。我尝试了3种代码替换方法:
我不明白为什么对象不会返回。
这是我的输出:
以下是代码:
主页组件
onSearch(){
this.indexingQuery(this.search); // for spliting the input user and check the word
let cek = this.dbService.checkTerm();
cek.then(
(data)=>{
for(var index in this.term) {
let a = this.dbService.ReadTermPerDocumentFreq(index);
console.log(a);
console.log("a");
this.termFA.push(a);
}
this.termFA.forEach(element=>{
console.log(element);
console.log(element.freq);
});
}
);}
数据库服务
ReadTermPerDocumentFreq(kata:string):TermFreqAll{
let temp:TermFreqAll ;
this.afDb.database.ref('/term_freq_all/'+kata).
once('value', function(snapshot){
let userData : any= snapshot.val();
temp = new TermFreqAll(
userData.word,
userData.doc_freq,
userData.freq,
userData.docs
);
console.log("temp");
console.log(temp);
console.log("temp");
});
console.log(temp);
return temp;}
如果我改变这样的代码。它会回来,但我无法获得财产。
这是输出:
代码:
主页组件
onSearch(){
this.indexingQuery(this.search);
let cek = this.dbService.checkTerm();
cek.then(
(data)=>{
for(var index in this.term) {
let a = this.dbService.ReadTermPerDocumentFreq(index);
this.termFA.push(a);
console.log(a);
console.log("a");
}
this.termFA.forEach(element=>{
console.log("Arrghh");
console.log(element);
console.log(element.freq);
});
}
);}
数据库服务
ReadTermPerDocumentFreq(kata:string):TermFreqAll{
let temp:TermFreqAll = new TermFreqAll();
this.afDb.database.ref('/term_freq_all/'+kata).
once('value', function(snapshot){
let userData : any= snapshot.val();
temp.freq = userData.freq;
temp.docs = userData.docs;
temp.doc_freq = userData.doc_freq;
temp.word = userData.word;
console.log("temp");
console.log(temp);
console.log("temp");
});
console.log(temp);
return temp;}
我尝试从数据库服务返回promise。该值可以推入TermFA数组但无法获取循环中的值。与第二次尝试相同的情况。它表示数组长度为0。
这是我的输出
这是代码:
主页组件
onSearch(){
this.indexingQuery(this.search);
let cek = this.dbService.checkTerm();
cek.then(
(data)=>{
for(var index in this.term) {
console.log(index);
let a = this.dbService.ReadTermPerDocumentFreq(index);
a.then((data)=>{
console.log("data");
console.log(data);
let b = this.termFA.push(data);
});
}
console.log("this.termFA");
console.log(this.termFA);
this.termFA.forEach(element=>{
console.log("Arrghh");
console.log(element);
console.log(element.freq);
});
console.log("this.termFA2");
console.log(this.termFA);
for(let i in this.termFA){
console.log("loop");
console.log(this.termFA[i]);
console.log(this.termFA[i].freq);
}
}
);}
数据库服务
ReadTermPerDocumentFreq(kata:string):Promise<TermFreqAll>{
return new Promise((resolve, reject)=>{
this.afDb.database.ref('/term_freq_all/'+kata).once("value",snapshot => {
let userData : any= snapshot.val();
// console.log(userData);
if (userData){
console.log("userdata");
console.log(userData);
let temp : TermFreqAll = new TermFreqAll();
temp.freq = userData.freq;
temp.docs = userData.docs;
temp.doc_freq = userData.doc_freq;
temp.word = userData.word;
// console.log("true");
resolve(temp);
}
else{
let temp : TermFreqAll = new TermFreqAll();
resolve(temp);
}
}).catch(error=>{
let temp : TermFreqAll = new TermFreqAll();
resolve(temp);
});
});}