我正在构建一个视频保存服务作为项目,并拥有从数据库response.array[]
获取的用户保存视频的数组,然后我将videoId
推送到显示数组video[]
但是在使用videoId
显示相应的console.log()
时,它仍然未定义。
我的代码段如下所示。
let video = [];
this.service.nextVideo().subscribe( response =>
{
response.array.forEach(element => {
video.push(element.videoId);
});
})
console.log(video[0]); //Output is undefined
完整的代码段就像这样
nxtVideo(){
let video = [];
this.service.nextVideo().subscribe( response =>
{
response.array.forEach(element => {
console.log(element.videoId);//Display's video id
video.push(element.videoId);
});
})
console.log(video[0]); //Output undefined
console.log(video); //Output shows all elements but on accessing specific elements it shows undefined
return (video[this.offset++]); //offset is a global variable to measure watched videos
}
Launch(){
let videoId = this.nxtVideo();
//The Display table starts here
}
答案 0 :(得分:0)
试试这段代码。它必须是有效的,因为它的异步功能。
let video = [];
this.service.nextVideo().subscribe( response =>
{
response.array.forEach(element => {
video.push(element.videoId);
});
console.log(video[0]); //Output is undefined
})