Categories

See More
Popular Forum

MBA (4887) B.Tech (1769) Engineering (1486) Class 12 (1030) Study Abroad (1004) Computer Science and Engineering (988) Business Management Studies (865) BBA (846) Diploma (746) CAT (651) B.Com (648) B.Sc (643) JEE Mains (618) Mechanical Engineering (574) Exam (525) India (462) Career (452) All Time Q&A (439) Mass Communication (427) BCA (417) Science (384) Computers & IT (Non-Engg) (383) Medicine & Health Sciences (381) Hotel Management (373) Civil Engineering (353) MCA (349) Tuteehub Top Questions (348) Distance (340) Colleges in India (334)
See More

Cloudkit JS && Node JS

General Tech Bugs & Fixes
Max. 2000 characters
Replies

usr_profile.png
Charles Kyobe

User

( 8 months ago )

 

I'm currently trying to perform server side connection to iCloud Server using the new CloudKit JS from Apple. According to the WWDC 2015 "CloudKit JS and Web Service", since CloudKit JS is a pure JS framework, you can use it in all JS environnements such as node JS.

I copied the source code of CloudKit JS from https://cdn.apple-cloudkit.com/ck/1/cloudkit.js and pasted it in a file named "cloudkit.js". Here is a demo of what I tried :

var CloudKit = require("/some/folders/cloudkit.js")

function demoPerformQuery() {
  CloudKit.configure({
      containers: [{
    containerIdentifier: 'myContainerIdentifier',
    apiToken: 'myAPIToken',
    environment: 'development'
      }]
    })

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

  publicDB.performQuery({recordType: 'Items'}).then(function(response){
    // never called :-(
  })
}

var express = require('express')

var app = express()

app.get("/", function(){
  demoPerformQuery()
})

var server = app.listen(8080, function () {
  console.log("server launched")
})

CloudKit seems to be correctly set up since all the functions are correctly called. But the callback of performQuery is never called. Why ?

Is there someone who already succeed to configure CloudKit JS in an server environnement ?

usr_profile.png
Jadav Payeng

User

( 8 months ago )

 

In the browser, CloudKit.js relies on XmlHttpRequest in order to fetch resources, but since CloudKit isn't an npm module you'll need a way to fetch things from your server.

npm install node-fetch

Using node-fetch, here is a tweaked version of your code that logs the resulting Items in your query:

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

CloudKit.configure({
  services: {
    fetch: fetch
  },
  containers: [{
    containerIdentifier: 'yourContainerIdentifier',
    apiToken: 'yourAPItoken',
    environment: 'development'
  }]
})

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

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

var express = require('express')
var app = express()

app.get("/", function() {
  demoPerformQuery()
})

var server = app.listen(8080, function () {
  console.log("Server listen")
})

what's your interest


forum_ban8_5d8c5fd7cf6f7.gif