REST examples/tutorials


#1

Hi,
I have big problems to understand the restnode. I can’t even get an example with the “Backend type”=“Prototype” to work. Can someone please present some examples?
Thanks, Philipp


#2

Take a look at this tutorial: http://docs.getnoodl.com/connecting-to-data.html


#3

Hello micart,
thanks for your answer. I have read all tutorials and forum-answers regarding this topic. Now I am trying to use a Google Firestore Database to get and set data. I was not able to do this with the Google Rest API and the Rest Node. -> Help appreciated.

My next try was a HTML-Content-Node, but the code inside the

<h1 id="lblQuote"></h1>

<input type="text" id="txtQuote" />
<button id="saveButton">SAVE</button>
<button id="loadButton">LOAD</button>
<!-- <script src="app.js"></script> -->
<script>

// Initialize Firebase
var config = {
apiKey: “AIzaSyApREa6-Q8hMQRRbEq6_jsjJsjwiowwiwX08”,
authDomain: “web-jnd.firebaseapp.com”,
databaseURL: “https://web-jnd.firebaseio.com”,
projectId: “web-jnd”,
storageBucket: “”,
messagingSenderId: “571823669”
};
firebase.initializeApp(config);

const outputHeader=document.querySelector("#lblQuote");
const inputTextField=document.querySelector("#txtQuote");
const saveButton=document.querySelector("#saveButton");
const loadButton=document.querySelector("#loadButton");

var firestore = firebase.firestore();

const docRef = firestore.doc(“samples/quote”);

saveButton.addEventListener(“click”,function(){
const textToSave=inputTextField.value;
console.log("Quotes "+textToSave);

docRef.set({
    inspirationalQuote:textToSave   
}).then(function(){
    console.log("Quote Save");
}).catch(function(error){
    console.log("Got an error: ",error);
});

});

loadButton.addEventListener(“click”,function(){
docRef.get().then(function(doc){
if(doc && doc.exists){
const myQuote=doc.data();
outputHeader.innerText="My Inspirational Quote: "+myQuote.inspirationalQuote;
}
}).catch(function(error){
console.log("Got an error: ",error);
});
});

//Real Time Data

getRealTimeUpdate=function(){
docRef.onSnapshot(function(doc){
if(doc && doc.exists){
const myQuote=doc.data();
console.log("Check out this document I received ",doc);
outputHeader.innerText="My Inspirational Quote: "+myQuote.inspirationalQuote;
}
});
}
getRealTimeUpdate();

</script>

#4

A simple example to fetch data from firebase with anonymous access.

https://drive.google.com/open?id=1EWylROiyZPQFZd2P5A7nz-c2xLaDlTT7

Please be gentle to the db if you plan to play with it :slight_smile:


#5

Hello Regi,
thank you very much. With your example I got mine running too. :smiley:


#6

Nice! If you get ‘posting’ to Firebase to work, please do share an example, as I haven’t looked into it, but would be very curious in the set-up.


#7

Hello Regi,
here is an example code for posting:

Rest Node:
Resource: /colors.json (in Regis example)
Method: PUT or POST (PATCH is possible with the code inside)
Endpoint: https://color-classification-93ad6.firebaseio.com

Code:
define({
// The input ports of the REST node, name of input and type
inputs:{
ExampleInput:‘number’,
},

// The output ports of the REST node, name of output and type
outputs:{
},

request:function(inputs,request) {
    
    //PUT or POST
    var text = '[' +
        '{ "ID":11 , "Priority":"10", "Text":"One" },' +
        '{ "ID":12 , "Priority":"20", "Text":"Two" },' +
        '{ "ID":13 , "Priority":"30", "Text":"' +
        this.inputs.ExampleInput + '" } ]';
    
    //UPDATE bzw. PATCH
    request.method = 'PATCH';
    text = '{ "1": { "ID":11 , "Priority":"10", "Text":"' + this.inputs.ExampleInput + '" } }';
    
    var obj = JSON.parse(text);

    request.content = obj;
},

response:function(outputs,response) {

}

})

Good Luck :slight_smile: