Javascript 角度推送对象到数组返回非函数
我试图将返回的数据从Javascript 角度推送对象到数组返回非函数,javascript,angular,typescript,websocket,Javascript,Angular,Typescript,Websocket,我试图将返回的数据从套接字io推送到我的消息数组中,但它显示: 错误类型错误:this.messages.push不是一个函数 代码 样本数据 from socket {id: 51, group_id: 1, user_id: 1, note: "wrwgwwg", deleted_at: null, …} created_at: "2020-05-18T08:19:59.000000Z" deleted_at: null group_
套接字io
推送到我的消息数组中,但它显示:
错误类型错误:this.messages.push不是一个函数
代码
样本数据
from socket
{id: 51, group_id: 1, user_id: 1, note: "wrwgwwg", deleted_at: null, …}
created_at: "2020-05-18T08:19:59.000000Z"
deleted_at: null
group_id: 1
id: 51
note: "wrwgwwg"
updated_at: "2020-05-18T08:19:59.000000Z"
user: {id: 1, name: "Sam", username: "admin", phone: "081200000001", photo: null, …}
user_id: 1
__proto__: Object
有什么想法吗?
更新
我的完整组件代码符合要求
import { Component, OnInit } from '@angular/core';
import { Plugins } from '@capacitor/core';
import { MenuController, LoadingController } from '@ionic/angular';
import { FormGroup, FormBuilder, Validators } from '@angular/forms';
import { AlertService } from 'src/app/Services/alert.service';
import { SendChatService } from 'src/app/Services/send-chat.service';
import { ActivatedRoute } from '@angular/router';
import { GroupsService } from 'src/app/Services/groups.service';
import { AuthService } from 'src/app/Services/auth.service';
import { User } from 'src/app/Services/user.service';
const { Toast } = Plugins;
// socket.io
import { Socket } from 'ngx-socket-io';
@Component({
selector: 'app-chat',
templateUrl: './chat.page.html',
styleUrls: ['./chat.page.scss'],
})
export class ChatPage implements OnInit {
newSegment: string;
public chat: FormGroup;
messages: any[] = [];
loading: any;
public user: User;
constructor(
private sendChatService: SendChatService,
private groupsService: GroupsService,
private menu: MenuController,
private alertService: AlertService,
public formBuilder: FormBuilder,
public loadingController: LoadingController,
private activatedRoute: ActivatedRoute,
private authService: AuthService,
private socket: Socket,
) {
this.menu.enable(true);
const id = this.activatedRoute.snapshot.paramMap.get('id');
this.chat = this.formBuilder.group({
newMessage: ['', Validators.required],
group_id: id
});
}
async ionViewDidEnter() {
(await this.authService.user()).subscribe(
user => {
this.user = user;
}
);
}
ionViewWillLeave() {
this.socket.disconnect();
}
ngOnInit() {
Toast.show({
text: 'Selamat Datang Ke grup chat.'
});
this.getData();
// socket.io
this.socket.connect();
// get back stored data form "sendMessage" and add it to the list
this.socket.fromEvent('message').subscribe((message: any) => {
console.log('from socket', message.msg.message);
this.messages.push(message.msg.message);
});
// end of socket.io
}
async getData() {
this.loading = await this.loadingController.create({
message: 'Please wait...',
spinner: 'crescent',
duration: 2000
});
await this.loading.present();
const id = this.activatedRoute.snapshot.paramMap.get('id');
this.groupsService.getGroupsDetails(id).subscribe(res => {
this.messages = res.data;
this.hideLoading();
});
}
private hideLoading() {
this.loading.dismiss();
}
doRefresh(event) {
console.log('Begin async operation');
setTimeout(() => {
console.log('Async operation has ended');
event.target.complete();
}, 2000);
}
ionViewWillEnter() {
this.newSegment = 'chats';
}
sendMessage() {
const chatt = this.chat.value;
this.sendChatService.messagesend(chatt.newMessage, chatt.group_id).subscribe(
(data: any) => {
this.alertService.presentToast(data.message);
console.log(data);
// this.messages.push(data);
// chatt.newMessage.reset();
// socket.io (send returned data to socket server - get it back in "ngOnInit")
this.socket.emit('send-message', { message: data.data });
},
error => {
this.alertService.presentToast(error.statusText);
console.log(error);
},
() => {
//
}
);
}
}
消息在数组的开头,但我看到这一行:
this.messages = res.data;
确保res.data确实是一个数组。或者这样做:
this.messages = [...res.data];
或:
或者修复后端:this.socket.fromEvent('message')…
我的消息中有notes
array inside(),我需要做的就是将新消息指向notes数组中,而不是消息本身
最后的代码是:
this.messages.notes.push(message.msg.message);
而不是
this.messages.push(message.msg.message);
你的代码看起来不错。你能告诉我们完整部件的代码吗?@mafortis我必须同意Marc的说法。我也像这样测试了你的代码,没有发现任何错误。@Mikefox2k好的,然后我共享所有代码。它是数组吗?你检查过它是否真的不是空的吗?(
res.data
)现在你明白我为什么说共享完整代码不是个好主意了吧?:)你不会像我预测的那样使用不相关的代码。我这边的一个建议是:永远不要使用任何代码。在哪一部分<代码>消息:任意[]=[]或.subscribe((消息:any)
?一个提示:重新命名,很快就会让人困惑。
this.messages.notes.push(message.msg.message);
this.messages.push(message.msg.message);