简体   繁体   中英

How to install CloudKit JS library via npm

I am currently working with the CloudKit JS API and doing a script with Node.

I installed the cloud kit library provided by Apple here by doing:

curl "https://cdn.apple-cloudkit.com/ck/2/cloudkit.js" > cloudkit.js

The script is the next one:

var fetch = require("node-fetch");
var CloudKit = require("./cloudkit.js");

CloudKit.configure({
    services: {fetch: fetch},
    containers: [{
        containerIdentifier: 'CONTAINER',
        apiToken: 'TOKEN',
        environment: 'development'
    }]
});

var container = CloudKit.getDefaultContainer();
container.setUpAuth();
var publicDB = container.publicCloudDatabase;

function demoPerformQuery() {
    publicDB.performQuery({recordType: 'Schedule'}).then(function(response){
        console.log(response)
    }).catch(function(error){
        console.log(error)
    })
}

demoPerformQuery();

The output when running it is:

(node:30096) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): [CKError
    ckErrorCode: AUTHENTICATION_FAILED
    extensionErrorCode: undefined
    isCKError: true
    isError: true
    isServerError: true
    isServerExtensionError: true
    message: no auth method found
    name: Error
    reason: no auth method found
    recordName: undefined
    redirectURL: undefined
    retryAfter: undefined
    serverErrorCode: AUTHENTICATION_FAILED
    subscriptionID: undefined
    uuid: 63b0ebe0-852a-454b-afc4-5a669e0a307c
    zoneID: undefined
]
{ Error: no auth method found
    at Function.value (/home/pi/Web/cloudkit.js:14:1432)
    at /home/pi/Web/cloudkit.js:13:27352
    at process._tickCallback (internal/process/next_tick.js:109:7)
  _ckErrorCode: 'AUTHENTICATION_FAILED',
  _uuid: '63a63eff-a962-4299-afcf-85c1a916f9ea',
  _reason: 'no auth method found',
  _serverErrorCode: 'AUTHENTICATION_FAILED',
  _extensionErrorCode: undefined,
  _retryAfter: undefined,
  _recordName: undefined,
  _subscriptionID: undefined,
  _zoneID: undefined,
  _redirectURL: undefined,
  message: 'no auth method found' }

As I've read in StackOverflow a solution for this might be to install it with npm but as a novice with Node I'm stuck where it says npm run-script install-cloudkit-js because I don't have that script and I don't know how to proceed.

It appears like your issue has to do with promises at container.setUpAuth();

Try changing your code so it looks more like this:

  var fetch = require('node-fetch');

  var CloudKit = require('./cloudkit');

  CloudKit.configure({
      services: {fetch: fetch},
      containers: [{
         containerIdentifier: 'CONTAINER',
         apiToken: 'TOKEN',
         environment: 'development'
     }]
  });

  var container = CloudKit.getDefaultContainer();
  var database = container.publicCloudDatabase; 

  container.setUpAuth()
    .then(function(userInfo){
      publicDB.performQuery({recordType:'Schedule'}).then(function(response){
         console.log(response);
    }).catch(function(error){
      console.log(error);
    })    
 })();

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