简体   繁体   English

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

[英]Uncaught You must specify a key using Parse.initialize

I've been using Parse for about three months but have started a new app. 我已经使用Parse大约三个月了,但是已经启动了一个新应用。 I am using Parse 1.4.2 js sdk. 我正在使用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. 该文件包含在main.js文件中。

When I try to deploy (using 1.4.2 sdk) I am getting the error: 当我尝试部署(使用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.

If I try to deploy using the latest sdk, I get the error: 如果我尝试使用最新的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)

If I comment out all of verification.js it will upload, but with it uncommented I get the error. 如果我注释掉所有的verification.js ,它将上传,但没有注释就收到错误。 My other working app never calls Parse.initialize . 我的其他工作应用程序从未调用过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 main.js

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

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 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. 这花了我大约3/4的时间才能找到答案。 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. 该问题似乎与使用export的方式有关。 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()尝试调用该方法,而不是将其设置为exports的属性。 exports.methodName = methodName also works fine. exports.methodName = methodName也可以正常工作。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 linkedin Uncaught错误:您必须指定一个有效的JavaScript API域作为此密钥的配置的一部分 - linkedin Uncaught Error: You must specify a valid JavaScript API Domain as part of this key's configuration 我是否应该在每个jQuery文件的顶部调用Parse.initialize() - Should I call Parse.initialize() on top of every jQuery file Javascript REST API - 您必须指定 API 密钥才能发出请求 - Javascript REST API - You must specify an API key to make request 您必须指定 &#39;to&#39; 属性 - You must specify 'to' property 您必须将有效的JavaScript API域指定为此密钥配置的一部分。 在LinkedIn - You must specify a valid JavaScript API Domain as part of this key's configuration. in LinkedIn Linkedin SDK您必须指定一个有效的JavaScript API域作为此密钥配置的一部分 - Linkedin SDK You must specify a valid JavaScript API Domain as part of this key's configuration 您必须在调用logIn之前初始化FacebookUtils - You must initialize FacebookUtils before calling logIn 您必须指定一个有效的JavaScript API域 - You must specify a valid JavaScript API Domain React和firebase setState&#39;您必须指定“ to”属性&#39; - React and firebase setState 'you must specify the “to” property' 错误 - 错误:必须在使用挂钩之前进行初始化 - error - Error: Must initialize before using hooks
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM