在请求npm的主体中传递数组或嵌套对象



我必须发送一些字符串类型的参数以及我体内的一个数组。但这给我带来了一个错误消息:

第一个参数必须是字符串或缓冲区

这是我的代码:

var tokenList = JSON.parse(req.body.tokenList);
    var mobParams = {
        "tokens": tokenList,
        "profile": "<myprofile>",
        "notification": {
            "title": req.body.title,
            "message": req.body.text
        }
    };
     request({
        method: "POST",
        url: 'https://api.ionic.io/push/notifications',
        headers: {
            "content-type": "application/json",
            "authorization": "Bearer ********"
        },
        body: (mobParams)
     }, function(error, response, body){
        console.log('Ionic push error', error);
        console.log('IOnic push res', response);
        console.log('IOnic push body', body);
        if(!error){
            return res.send({
                code: 1,
                message: "success"
            });
        }else{
            return res.send({
               code: 0,
                message: error
            });
        }

如何将数组传递到此对象中以请求NPM?

另外,我想补充一点,该实现在前端可以很好地工作,但是我有单独的代码库,这要求我点击多个FCM请求,即在循环中。因此,我很乐意有一个解决方案,因为离子推动和FCM推动都没有工作

对于FCM推动,我正在尝试下面的代码:

let desktopParams = {
                                        "notification": {
                                            "title": 'Merchant Portal Notifications',
                                            "body": req.body.text
                                            // "click_action" : action
                                        },
                                        "to": '/topics/' + topic
                                    };
                                     request({
                                        method: "POST",
                                        json: true,
                                        url: 'https://fcm.googleapis.com/fcm/send',
                                        headers: {
                                            "content-type": "application/json",
                                            "authorization": "key=****"
                                        },
                                        body: desktopParams
                                    }, function(error, response, body){
                    console.log('error', error);
                    console.log('response', response);
                    console.log('body', body);

                                        //return body;
                                    });

您应该尝试将tokenList&amp;req.body.text与字符串一起加入之前(尝试将其记录并发布结果,以便人们对对象有更好的了解...(:


var cho = [{name:'john',lname:'cena'},{name:'mary',lname:'jane'}];
var che = {list:[{name:'john',lname:'cena'},{name:'mary',lname:'jane'}],group:'people'}

var mobParams = {
        "tokens":JSON.parse(JSON.stringify(cho)),
        "profile": "<myprofile>",
        "notification": {
            "title": "Some title",
            "message":JSON.parse(JSON.stringify(che))
        }
    };

console.log(JSON.stringify(mobParams));//----->{"tokens":[{"name":"john","lname":"cena"},{"name":"mary","lname":"jane"}],"profile":"<myprofile>","notification":{"title":"Some title","message":{"list":[{"name":"john","lname":"cena"},{"name":"mary","lname":"jane"}],"group":"people"}}}

var arr = [{name:'john',lname:'cena'},{name:'mary',lname:'jane'}]

var js = JSON.stringify(arr);
var blabla = {'item':'something',js}
console.log(blabla); //-----> Object {item: "something", js: "[{"name":"john","lname":"cena"},{"name":"mary","lname":"jane"}]"}

var js = JSON.parse(JSON.stringify(arr));
var blabla = {'item':'something',js}
console.log(blabla); //-----> Object {item: "something", js: Array(2)}

var js = JSON.parse(arr);
var blabla = {'item':'something',js}
console.log(blabla); //-----> "SyntaxError: Unexpected identifier"

最新更新