2017-07-19 171 views
0

我目前正在使用CloudKit JS API并使用Node做脚本。如何通过npm安装CloudKit JS库

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

剧本是下一个:

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

运行它时的输出:

我做安装苹果here提供的云套件库

(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' } 

正如我在StackOverflow中读到的解决方案可能是安装它与npm但作为新手与节点我卡在它说,因为我没有该脚本,我不知道如何继续。

回答

0

它看起来像你的问题已经在container.setUpAuth();

与承诺做试着改变你的代码,以便它看起来更像是这样的:

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