2014-10-17 56 views
0

在下面的云功能中获取错误。indexOf在通过云解析时出现未定义错误

NSMutableDictionary * params = [NSMutableDictionary new]; 

    params[@"user"] = _sender_ID; 
    params[@"recipientId"] = _owner_ID; 
    params[@"message"] = msgToSend; 
    [PFCloud callFunctionInBackground:@"sendPushToUser" withParameters:params block:^(id object, NSError *error) { 
     if (!error) { 
      // Push sent successfully 

      NSLog(@"msg posted!"); 
     } 
    }]; 

错误:

Error: TypeError: Cannot call method 'indexOf' of undefined 
at main.js:15:35 (Code: 141, Version: 1.2.20) 

代码main.js如下。

Parse.Cloud.define("sendPushToUser", function(request, response) { 
var senderUser = request.user; 
var recipientUserId = request.params.recipientId; 
var message = request.params.message; 

// Validate that the sender is allowed to send to the recipient. 
// For example each user has an array of objectIds of friends 
if (senderUser.get("friendIds").indexOf(recipientUserId) === -1) { 
response.error("The recipient is not the sender's friend, cannot send push."); 
} 

    // Validate the message text. 
    // For example make sure it is under 140 characters 
    if (message.length > 140) { 
    // Truncate and add a ... 
message = message.substring(0, 137) + "..."; 
    } 

// Send the push. 
// Find devices associated with the recipient user 
var recipientUser = new Parse.User(); 
recipientUser.id = recipientUserId; 
var pushQuery = new Parse.Query(Parse.Installation); 
pushQuery.equalTo("user", recipientUser); 

// Send the push notification to results of the query 
Parse.Push.send({ 
where: pushQuery, 
data: { 
    alert: message 
    } 
    }).then(function() { 
    response.success("Push was sent successfully.") 
    }, function(error) { 
    response.error("Push failed to send with error: " + error.message); 
    }); 
}); 

以上是写在云main.js代码得到了下面的链接相同的。据我所知,从错误是问题是与friendIds这就是为什么它不调用方法'indexOf'。

从这个链接中获得了想法blog

+0

'main.js'? 'indexOf'? msgToSend的内容? – 2014-10-17 14:07:17

+0

检查博客链接以了解这些条款。 – 2014-10-17 14:19:52

+0

'main.js'没有在那里定义,因此'indexOf'的使用不能被识别。 *你的*代码使用'msgToSend',它的内容你不提供。 – 2014-10-17 14:43:45

回答

0

看起来,与在评论中声称的相反,senderUser(即request.user)事实上并不存在存储在friendIds中的一组朋友。

如果没有关于senderUser的内容的任何信息(例如,您可能刚刚拼错了字段的名称),那么我们可以做的最好的做法是通过在生成if之前添加该错误来更好地回应错误错误:

if (senderUser.get("friendIds") === undefined) { 
    response.error("The sender does not have any friends, cannot send push."); 
} 
相关问题