2017-06-03 144 views
0

我必须发送String类型的一些参数以及我身体中的一个数组。 但它抛出我的错误消息:在请求正文中传递数组或嵌套对象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; 
            }); 
+0

尽量'体:JSON.stringify(mobParams)' – Ajay

+0

试过。通话无法解决。没有错误,没有任何控制台。 – kushalvm

+0

在请求头中添加'“Content-Length”:JSON.stringify(mobParams).length' – Ajay

回答

0

你应该尝试字符串化的tokenList & 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"