首页 > 解决方案 > 科尔多瓦应用程序中的警报功能返回 [object, Object]

问题描述

我正在尝试在我的科尔多瓦应用程序中使用科尔多瓦本地通知插件,它工作正常,但返回预定通知的功能在警报框中显示 [object, Object],这是我尝试过的

var callbackOpts = function(notifications) {
    //.log(notifications);
    navigator.notification.alert(notifications.length === 0 ? '- none -' : notifications.join(', '));
    var count = notifications.length;
    document.getElementById("reminderCount").innerHTML = count;
};

function getScheduled() {
    cordova.plugins.notification.local.getScheduled(callbackOpts);
    document.getElementById("reminderCount").innerHTML = count;
}

请帮忙 这是通知格式

 cordova.plugins.notification.local.schedule({
            id: btn.dataset.id,
            title: btn.dataset.name,
            text: "Your Bus Will Be At The Station In " + changedTime + " Minutes",
            trigger: { in: btn.dataset.diff, unit: 'minute' },
            sound: "file://js/when.mp3",
            icon: "file://img/mmArtboard.png"
            });

标签: javascriptcordova

解决方案


那是因为当您alert()在显示之前使用对象作为参数 调用时.toString(),将在其上调用方法。

>>[{id: 1, title: hello}].toString()
"[object Object]"

如果要按原样显示对象,可以调用JSON.strignify(notifications)它,以便将其转换为 js 对象的字符串表示形式。

如果你想只显示对象数组中的标题,你可以只提取标题并加入它们并显示它。

>>let notifications = [{ id: 1, title: "title-1"}, { id: 1, title: "title-2"} ]
>>notifications.map((notification)=> notification.title ).join(' , ')
"title-1 , title-2 "

推荐阅读