loading
open in
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
const Appbase = require("appbase-js");

// Create appbase instance to use methods
const appbaseRef = Appbase({
  url: "https://scalr.api.appbase.io",
  app: "housing-demo",
  credentials: "YnCgVwozV:e9ec5cb0-58d8-4bb0-badc-acaf726661f1"
});

// Subscribe to streaming data by id
appbaseRef.getStream(
  {
    type: "listing",
    id: "H1"
  },
  data => {
    // "data" handler is triggered every time there is a **new** document update.
    console.log(data);
  },
  error => {
    console.log("caught a stream error", error);
  }
);

// Updated house data object
const jsonObject = {
  name: 'The Dank House',
};

// Update data of same `id` with update method
// Also adding a small delay before updating data
setTimeout(() => {
  appbaseRef.update({
    type: "listing",
    id: "H1",
    body: {
      doc: jsonObject
    }
  }).then(response => {
    console.log("Success: ", response);
  }).catch(error => {
    console.log("Error: ", error);
  });
}, 200);
node v9.7.1 linux/amd64