2017-06-03 51 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"