未捕获您必须使用Parse.initialize指定密钥

时间:2015-09-25 09:26:40

标签: javascript parse-platform deployment sdk

我已经使用Parse大约三个月但已经开始使用新的应用程序了。我正在使用Parse 1.4.2 js sdk。

我在一个单独的文件中有一些代码,它向数据库发出请求。该文件包含在main.js文件中。

当我尝试部署(使用1.4.2 sdk)时,我收到错误:

Uploading source files
Uploading recent changes to scripts...
The following files will be uploaded:
/Users/aaron/dev/Familiar/cloud/main.js
/Users/aaron/dev/Familiar/cloud/user.js
/Users/aaron/dev/Familiar/cloud/verification.js
Finished uploading files
Uncaught You must specify a key using Parse.initialize.

如果我尝试使用最新的sdk进行部署,我会收到错误:

Uploading source files
Uploading recent changes to scripts...
The following files will be uploaded:
/Users/aaron/dev/Familiar/cloud/verification.js
Finished uploading files
TypeError: Object #<Object> has no method 'request'
    at Object.ajaxMod [as ajax] (<anonymous>:925:19)
    at e.<anonymous> (Parse.js:13:25717)
    at e.s (Parse.js:12:26759)
    at Parse.js:12:27145
    at i (Parse.js:12:27100)
    at e.n.value (Parse.js:12:27130)
    at Object.y.request (Parse.js:13:25644)
    at Object.u.default.setQueryController.find (Parse.js:13:6132)
    at e.a.value (Parse.js:13:76)
    at e.<anonymous> (verification.js:18:38)

如果我注释掉所有verification.js它会上传,但如果没有注释,我会收到错误消息。我的其他工作应用从不致电Parse.initialize。为什么现在要求它?

目录结构:

├── cloud
│   ├── comment.js
│   ├── lib
│   │   ├── config.js
│   │   ├── helpers.js
│   │   └── md5.js
│   ├── main.js
│   ├── user.js
│   └── verification.js
├── config
│   └── global.json
└── public
    └── index.html

main.js

require("cloud/user.js");
require("cloud/comment.js");
require("cloud/verification.js");

verification.js

var conf = require("cloud/lib/config.js").conf;
var helpers = require("cloud/lib/helpers.js");

function createSmsVerification(user) {
    return getUserSmsVerification(user)
        .then(function (verification) {
            var verificationQuery = new Parse.Query("Verification");
            return verificationQuery.first(); <-- this is the line causing the error
        })
        .then(function (verification) {
            return verification;
        }, function (error) {
            return Parse.Promise.error("createSmsVerification - " + error.message);
        });
}
exports.createSmsVerification = createSmsVerification();

function getUserSmsVerification(user) {
    return Parse.Promise.as();
}

user.js的

var helpers         = require("cloud/lib/helpers.js"),
    conf            = require("cloud/lib/config.js").conf,
    sms             = require("cloud/sms.js"),
    verification    = require("cloud/verification.js");


Parse.Cloud.define("register", function(request, response){
    Parse.Cloud.useMasterKey();
    var phoneNumber = request.params.phoneNumber;
    var user;
    var userQuery = new Parse.Query(Parse.User);
    userQuery.equalTo("username", phoneNumber);

    return userQuery
        .first(function(userResult){
            if(!helpers.isDefined(userResult)){
                userResult = createNewUser(phoneNumber);
            }else{
                userResult.set("smsVerified", false);
            }
            return userResult.save();
        })
        .then(function(userResult){
            user = userResult;
            return verification.createSmsVerification(user);
        })
        .then(function(){
            response.success();
        }, function (error){
            console.error("register - " + error.message);
            response.error(error.message);
        });
});

function createNewUser(phoneNumber){
    var newUser = new Parse.User();
    newUser.set("username", helpers.phoneToUsername(phoneNumber));
    newUser.set("password", helpers.generatePassword(phoneNumber));
    newUser.set("smsVerified", false);
    return newUser;
}

2 个答案:

答案 0 :(得分:2)

我有同样的问题,我发现每当我尝试在全局级别调用函数时,我都会遇到同样的错误。我能够调用函数的唯一时间是在其他函数的范围内。

与您的代码有关,我想您正在尝试导出函数本身,例如

exports.createSmsVerification = createSmsVerification;

您拥有的代码:

exports.createSmsVerification = createSmsVerification();

实际调用该函数并导出结果。

答案 1 :(得分:1)

这花了我大约3/4的时间来找到答案。它不在文档中。错误根本不明确。

问题似乎与export的使用方式有关。我假设使用exports.methodName = methodName()试图调用该方法,而不是将其设置为exports的属性。 exports.methodName = methodName也可以。

以下代码不工作

function createSmsVerification(user) {
    return getUserSmsVerification(user)
        .then(function (verification) {
            var verificationQuery = new Parse.Query("Verification");
            return verificationQuery.first(); <-- this is the line causing the error
        })
        .then(function (verification) {
            return verification;
        }, function (error) {
            return Parse.Promise.error("createSmsVerification - " + error.message);
        });
}
exports.createSmsVerification = createSmsVerification();

以下代码工作

exports.createSmsVerification = function(user) {
    return getUserSmsVerification(user)
        .then(function (verification) {
            var verificationQuery = new Parse.Query("Verification");
            return verificationQuery.first();
        })
        .then(function (verification) {
            return verification;
        }, function (error) {
            return Parse.Promise.error("createSmsVerification - " + error.message);
        });
}