我正在開發一個帶有離子/角度和 laravel 后端的聊天應用程式。我無法將服務器回應推送到現有陣列中。
從服務器獲取所有聊天訊息
getConversations(standardId) {
this.data.getConversations(standardId).subscribe(data => {
this.messages = data;
this.logScrollEnd();
console.log(data);
}, err => {
console.log(err);
});}
這是我呼叫 getConversations() 后收到的物件
{
"28-12-2021": [
{
"id": 23,
"message": "hi there",
"user_id": 6,
"standard_id": 6,
"message_type": "text",
"created_at": "2021-12-28T08:17:28.000000Z",
"updated_at": "2021-12-28T08:17:28.000000Z",
"user": {
"id": 6,
"name": "test school2 owner",
"email": "[email protected]",
"mobile": "1234567867",
"email_verified_at": null,
"deleted_at": null,
"created_at": "2021-12-28T08:13:19.000000Z",
"updated_at": "2021-12-28T08:13:19.000000Z",
"roles": [
{
"id": 1,
"role": "school",
"created_at": null,
"updated_at": null,
"pivot": {
"user_id": 6,
"role_id": 1
}
}
]
},
"standard": {
"id": 6,
"standard_type_id": 1,
"name": "LKG Div A",
"school_id": 3,
"online_link": "",
"online_link_type": "",
"created_at": "2021-12-28T08:13:19.000000Z",
"updated_at": "2021-12-28T08:13:19.000000Z"
}
},
{
"id": 24,
"message": "another message",
"user_id": 6,
"standard_id": 6,
"message_type": "text",
"created_at": "2021-12-28T08:17:28.000000Z",
"updated_at": "2021-12-28T08:17:28.000000Z",
"user": {
"id": 6,
"name": "test school2 owner",
"email": "[email protected]",
"mobile": "1234567867",
"email_verified_at": null,
"deleted_at": null,
"created_at": "2021-12-28T08:13:19.000000Z",
"updated_at": "2021-12-28T08:13:19.000000Z",
"roles": [
{
"id": 1,
"role": "school",
"created_at": null,
"updated_at": null,
"pivot": {
"user_id": 6,
"role_id": 1
}
}
]
},
"standard": {
"id": 6,
"standard_type_id": 1,
"name": "LKG Div A",
"school_id": 3,
"online_link": "",
"online_link_type": "",
"created_at": "2021-12-28T08:13:19.000000Z",
"updated_at": "2021-12-28T08:13:19.000000Z"
}
}
],
"29-12-2021": [
{
"id": 24,
"message": "hi",
"user_id": 6,
"standard_id": 6,
"message_type": "text",
"created_at": "2021-12-29T13:31:45.000000Z",
"updated_at": "2021-12-29T13:31:45.000000Z",
"user": {
"id": 6,
"name": "test school2 owner",
"email": "[email protected]",
"mobile": "1234567867",
"email_verified_at": null,
"deleted_at": null,
"created_at": "2021-12-28T08:13:19.000000Z",
"updated_at": "2021-12-28T08:13:19.000000Z",
"roles": [
{
"id": 1,
"role": "school",
"created_at": null,
"updated_at": null,
"pivot": {
"user_id": 6,
"role_id": 1
}
}
]
},
"standard": {
"id": 6,
"standard_type_id": 1,
"name": "LKG Div A",
"school_id": 3,
"online_link": "",
"online_link_type": "",
"created_at": "2021-12-28T08:13:19.000000Z",
"updated_at": "2021-12-28T08:13:19.000000Z"
}
}
]}
我在前端使用 laravel echo
echo.private(`standards.${this.userStandardId}`)
.listen('NewMessage', (resp) => {
console.log('joined standards.', this.userStandardId);
console.log('response: ', resp);
this.messages.push(resp);
this.logScrollEnd();
})
這是我提交新聊天時從 laravel echo 得到的回應
{
"message": {
"id": 55,
"message": "hi there",
"user_id": 6,
"standard_id": 6,
"message_type": "text",
"created_at": "2022-01-07T20:31:57.000000Z",
"updated_at": "2022-01-07T20:31:57.000000Z",
"user": {
"id": 6,
"name": "test school2 owner",
"email": "[email protected]",
"mobile": "1234567867",
"email_verified_at": null,
"deleted_at": null,
"created_at": "2021-12-28T08:13:19.000000Z",
"updated_at": "2021-12-28T08:13:19.000000Z",
"roles": [
{
"id": 1,
"role": "school",
"created_at": null,
"updated_at": null,
"pivot": {
"user_id": 6,
"role_id": 1
}
}
]
},
"standard": {
"id": 6,
"standard_type_id": 1,
"name": "LKG Div A",
"school_id": 3,
"online_link": "",
"online_link_type": "",
"created_at": "2021-12-28T08:13:19.000000Z",
"updated_at": "2021-12-28T08:13:19.000000Z"
}
}}
我想將回應推送到上述陣列。但是當我嘗試這個時得到一個錯誤
this.messages.push(resp);
錯誤型別錯誤:this.messages.push 不是函式
請幫我解決這個問題,在此先感謝
uj5u.com熱心網友回復:
由于 getConversations 回應的新元素有一個鍵作為訊息的 created_at 值的日期,我們將以該格式創建新日期(當我們得到回應時在 echo 中):
let day = resp.message.created_at.slice(8,10);
let month = resp.message.created_at.slice(5,7);
let year = resp.message.created_at.slice(0,4);
let newDate = day '-' month '-' year;
然后我們將訊息創建一個新陣列并將其作為值添加到物件中的 newDate 鍵中:
this.messages[newDate] = [resp.message];
但是,如果 key(newDate) 下有(或預計會有)更多訊息,我們需要推送一個物件:
this.messages[newDate].push(resp.message);
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/406212.html
標籤:
