javascript array.map函数不工作
我正在使用laravel和vuejs创建聊天应用程序。我已经在laravel中加密了我的响应,因为我不希望任何人也在控制台中看到我的响应:-javascript array.map函数不工作,javascript,laravel,vue.js,Javascript,Laravel,Vue.js,我正在使用laravel和vuejs创建聊天应用程序。我已经在laravel中加密了我的响应,因为我不希望任何人也在控制台中看到我的响应:- public function get() { $contacts = User::where('id', '!=', auth()->id())->get(); $unreadIds = Message::select(\DB::raw('`from` as sender_id,count(`from`) as messag
public function get()
{
$contacts = User::where('id', '!=', auth()->id())->get();
$unreadIds = Message::select(\DB::raw('`from` as sender_id,count(`from`) as messages_count'))
->where('to', auth()->id())
->where('read', false)
->groupBy('from')
->get();
$contacts = $contacts->map(function($contact) use ($unreadIds) {
$contactUnread = $unreadIds->where('sender_id', $contact->id)->first();
$contact->unread = $contactUnread ? $contactUnread->messages_count : 0;
return $contact;
});
$contacts = base64_encode($contacts);
return response()->json($contacts);
}
现在,当我希望访问此值以在vue js中显示数据时,我正在执行以下操作:-
axios.get(this.globalUrl+'/contacts')
.then((response) => {
let encryptData = atob(response.data);
console.log(encryptData);
//data received after de-converting
/*[{"id":2,"phone":null,"name":"Casimir Morar MD","email":"clint.haag@hartmann.info","profile_image":null,"created_at":null,"updated_at":null,"unread":0},{"id":3,"phone":null,"name":"Lina Prosacco","email":"okeefe.camila@gmail.com","profile_image":null,"created_at":null,"updated_at":null,"unread":0},{"id":4,"phone":null,"name":"Miss Aglae Emard DDS","email":"qcarter@yahoo.com","profile_image":null,"created_at":null,"updated_at":null,"unread":0},{"id":5,"phone":null,"name":"Demarco Kilback","email":"kessler.coy@swift.com","profile_image":null,"created_at":null,"updated_at":null,"unread":0},{"id":6,"phone":null,"name":"Tyrell Ziemann Sr.","email":"clementina.kautzer@price.org","profile_image":null,"created_at":null,"updated_at":null,"unread":0},{"id":7,"phone":null,"name":"Ella Hand","email":"areichel@watsica.com","profile_image":null,"created_at":null,"updated_at":null,"unread":0},{"id":8,"phone":null,"name":"Dr. Maxie O'Hara DDS","email":"wallace31@yahoo.com","profile_image":null,"created_at":null,"updated_at":null,"unread":0},{"id":9,"phone":null,"name":"Mrs. Mattie Monahan IV","email":"ernser.pasquale@hotmail.com","profile_image":null,"created_at":null,"updated_at":null,"unread":0},{"id":10,"phone":null,"name":"Bradly Crona","email":"lehner.cordie@pagac.com","profile_image":null,"created_at":null,"updated_at":null,"unread":0},{"id":11,"phone":null,"name":"Orland Kihn","email":"jacobs.wyman@yahoo.com","profile_image":null,"created_at":null,"updated_at":null,"unread":0}]*/
let finalArray = encryptData.map(function (obj) {
return obj;
//this.contacts.push(obj);
});
console.log(finalArray);
this.contacts = finalArray;
});
在这里,我将encryptData变量中的数据作为一个对象数组获取,但每次获取此错误时,我都会继续操作:-
Uncaught (in promise) TypeError: encryptData.map is not a function
请帮助我找到解决方案,谢谢。您可能错过了一个电话:
基本上:
- PHP脚本以JSON格式返回base64编码的数据
的回调将此作为base64编码字符串接收axios.get
base64解码字符串(此时它仍然是一个字符串)atob
将其转换回实际数据JSON.parse
- PHP脚本以JSON格式返回base64编码的数据
的回调将此作为base64编码字符串接收axios.get
base64解码字符串(此时它仍然是一个字符串)atob
将其转换回实际数据JSON.parse
- 您可能错过了一个电话:
基本上:
console.log(encryptData)
atob()做什么?@Nguy的结果ễNthanthuú我编辑了问题并添加了回答。请喝一杯look@informant09它将数据重新转换为原始形式。我已经在LaravelTank中的base64_encode()的帮助下转换了数据。感谢你这么做,但你能解释一下使用JSON背后的逻辑吗。ParseThank感谢你的知识和信息。感谢你这么做,但你能解释一下使用JSON背后的逻辑吗。ParseThank感谢你的知识和信息。
axios.get(this.globalUrl + '/contacts')
.then((response) => {
let encryptData = JSON.parse(atob(response.data));
let finalArray = encryptData.map(function(obj) {
// ...
});
this.contacts = finalArray;
});