Parse JS SDK:不能使用主密钥,它没有提供



我需要在我的angular2应用程序中使用masterKey,但我不能将其传递给initialize函数,我无法谷歌出原因。源自package.json: "parse": "~1.9.2" .

初始化:

import {Parse} from '~/node_modules/parse/dist/parse';
@Injectable()
export class TFCloudService {
    constructor() {
        this.parse = Parse;
        Parse.initialize(appConfig.parse.appId, null, appConfig.parse.masterKey);
        Parse.serverURL = appConfig.parse.clientServerUrl;
        Parse.liveQueryServerURL = appConfig.parse.liveQueryServerURL;
    }
}
错误来源:

this.edittedUser.save(null, {useMasterKey: true})
.then((user) => {
    console.log(user);
});

错误文本:Error: Cannot use the Master Key, it has not been provided.

appConfig.parse.masterKey工作得很好,我也用硬编码键检查了那行,但得到了相同的结果。

实际上猜到了传递密钥的正确方法:

Parse.initialize(appConfig.parse.appId);
Parse.masterKey = appConfig.parse.masterKey;

按照惯例,你永远不应该在前端使用主密钥。

因此,正确的方法应该是为需要主密钥的操作创建云函数
const Parse = require('parse/node');
Parse.serverURL = 'https://parseapi.back4app.com'; // This is your Server URL
// Remember to inform BOTH the Back4App Application ID AND the JavaScript KEY
Parse.initialize(
  'APP_ID', // This is your Application ID
  'JS_KEY', // This is your Javascript key
  'MASTER_KEY' // This is your Master key (never use it in the frontend)
);
Parse.Cloud.define("hello", async (request) => {
    const query = new Parse.Query("SomeClass");
    const results = await query.distinct("field"); // needs Master Key
    // ...
});

然后在浏览器应用程序中调用它

<script type="text/javascript" src="https://npmcdn.com/parse/dist/parse.min.js"></script>
<script type="text/javascript" type="text/javascript">
    Parse.initialize(
        "APP_ID",
        "JS_KEY"
    );
    Parse.serverURL = 'https://parseapi.back4app.com/'
    const helloFunction = await Parse.Cloud.run("hello");
</script>

最新更新