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;
});
儘量'體:JSON.stringify(mobParams)' – Ajay
試過。通話無法解決。沒有錯誤,沒有任何控制檯。 – kushalvm
在請求頭中添加'「Content-Length」:JSON.stringify(mobParams).length' – Ajay