[英]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.