简体   繁体   中英

Uncaught You must specify a key using Parse.initialize

I've been using Parse for about three months but have started a new app. I am using Parse 1.4.2 js sdk.

I have some code in a separate file which is making a request to the database. The file is included in the main.js file.

When I try to deploy (using 1.4.2 sdk) I am getting the error:

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.

If I try to deploy using the latest sdk, I get the error:

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)

If I comment out all of verification.js it will upload, but with it uncommented I get the error. My other working app never calls Parse.initialize . Why is it asking for it now?

Directory structure:

├── 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;
}

I had the same issue and I found that I'd hit the same error whenever I tried to call a function at the global level. The only time I've been able to call functions is within the scope of other functions.

Pertaining to your code, I imagine you're trying to export the function itself eg

exports.createSmsVerification = createSmsVerification;

The code you have:

exports.createSmsVerification = createSmsVerification();

actually calls the function and exports the result.

This took me about 3/4 of a day to find the answer. It is not in the documentation. The error is not clear at all.

The problem seems to be related to the way export is used. I assume that using exports.methodName = methodName() is trying to call the method, rather than set it as an attribute of exports . exports.methodName = methodName also works fine.

The following code DOES NOT WORK

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();

The following code DOES WORK

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);
        });
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM