Data being duplicated when the update function is triggered - Firebase - javascript

I'm making a web app and trying to update data on Firebase. But when the update function is being triggered, data is being duplicated on the database. I want data to be updated only inside voter/voterId. I don't want the same data to be duplicated inside voter/. Here's my code:
// Initialize Firebase
firebase.initializeApp(firebaseConfig);
var voterid, name, nic, mobile;
function Ready(){
voterid = document.getElementById("voterid").value;
name = document.getElementById("name").value;
nic = document.getElementById("nic").value;
mobile = document.getElementById("mobile").value;
}
document.getElementById("register").onclick = function(){
Ready();
if (voterid != null){
firebase.database().ref('voter/'+voterid).update({
name : name,
nic : nic,
mobile :mobile
});
}
}

It looks like voterid is an empty string, which you don't check for.
if (voterid != null && voterid.length > 0){
firebase.database().ref('voter/'+voterid).update({
name : name,
nic : nic,
mobile :mobile
});
}

Related

How to get user infomation with uid in firebase?

What I want to make
A system in which users send messages to administrators.
Progress
get uid (success)
get user info (fail)
If the user leaves a message, the administrator wants to see a list of them.
In the database,
messages {
uid1 {
key1 {
name : "user name"
text : "text that user remained"
created_at : "time that message created"
}
key2 {
…
}
…
}
uid2 {
…
}
}
I store the information as above. now be able to view this information as a list on the admin page..(and admin click on the list to see the message.)
However, I have succeeded in loading each uid, but I do not know how to use `uid 'to fetch the user's information.
Here is the output(now):
<div>uid1</div>
<div>uid2</div>
Here is the code:
function getListItem() {
var messagesRef = database.ref('personalchats') //personalchats 에 있는 uid로 접근
messagesRef.on('child_added', setListItem);
messagesRef.on('child_changed', setListItem);
}
function setListItem(data) {
var key = data.key;
var html = '<div class="listitem"> ' + key + '</div>;
$(".collection").append(html);
}
I want this result to be:
<div>userName1 : current message</div>
<div>userName2 : current message</div>
...
What can I do for it?
Thank you.
convert the data using data() and then extract it using the value names given in the database.
function getListItem() {
var messagesRef = database.ref('personalchats') //personalchats
messagesRef.on('child_added', setListItem);
messagesRef.on('child_changed', setListItem);
}
function setListItem(data) {
var key = data.key;
// create a variable to store the data
var dataVal=data.val();
var msgtext=dataVal.text;
var html = '<div class="listitem"> ' + key + ': '+ msgtext</div>;
$(".collection").append(html);
}

How to update on node js using sqlite3?

I would like to ask how to update on sqlite3 node js, the first function is working (views the data in the HTML from the database), my problem is how to update it. Thanks!
exports.post = function(req, res){
var id = JSON.stringify(req.body.id);
var inputData = [req.body.GIVENNAME, req.body.SURNAME, id];
db.run("UPDATE f11 SET GIVENNAME=$GIVENNAME, SURNAME=? WHERE id=?",inputData,
{
$GIVENNAME : GIVENNAME,
$SURNAME : SURNAME,
});
res.redirect("/legone/survey/surveyform/form11");
};
Update :
You can try running your query like this:
var inputData = [req.body.GIVENNAME, req.body.SURNAME, id];
db.run("UPDATE f11 SET GIVENNAME=?, SURNAME=? WHERE id=?",inputData,function(err,rows){
....
});
Hope this helps!

Trying to use Local Storage for Reg/Login

I am trying to use local storage to make a basic registration and login screen.
I have managed to get all the basics running but I can't get the fields to save to the local storage to them pull for the login process. I do not believe the HTML is wrong but the Javascript.
Here is the JS:
function save() {
var inputUserName = document.getElementById('regusername');
var inputPassWord = document.getElementById('regpassword');
var inputEmail = document.getElementById('regemail');
localStorage.setItem('username', inputUserName.value);
localStorage.setItem('email', inputEmail.value);
localStorage.setItem('password', inputPassWord.value);
}
function check() {
// Getting data from the register-form
var inputUserName = localStorage.getItem('username');
var inputPassWord = localStorage.getItem('password');
var username = document.getElementById('username');
var password = document.getElementById('password');
if (username.value == inputUserName && password.value == inputPassWord) {
alert('You have successfully logged in' + inputUserName);
} else {
alert('ERROR')
for(var i = 0; i < localStorage.length; i++){
alert(localStorage.key(i));
}
}
}
I figured it out, it was my HTML I had a name="Name" and it seemed to collide with the input that the user had changing to whatever was in the name="" in this case being Name, Email and Password.
If you want to get the input of the user in a specific field, you need to use something like:
document.getElementById('username').value;
You forgot to add the .value at the end.

Jquery - fill input if user exist

I'm trying to fill a form if I find that the name and the firstname input by the user already exist in my database.
For the moment, I would like to check if the name and the firstname already exist in the database but my javascript program doesn't work. It's seems that the part..." "$.post("../modules/verifier_staff.php",{ nom: ..." is not correct.
Then I would like to fill my form with the data that my database give (array).
Can somenone help me to find my mistakes and to tell me how I can use the data i receive from database to fill my form?
$(document).ready(function(){
var form_nom = $("#staff_nom");
var form_prenom = $("#staff_prenom");
$(form_nom).change(function() {
checkvariable();
});
$(form_prenom).change(function() {
checkvariable();
});
function checkvariable(){
if((form_nom.val().length >0)&&(form_prenom.val().length>0)){
prenom_staff = form_prenom.val();
nom_staff = form_nom.val();
//alert(nom_staff);
$.post("../modules/verifier_staff.php",{ nom: nom_staff, prenom: prenom_staff},
function(data){
if(data)
{
alert('ok');
}
else alert('nok');
});
}
}
});
var form_nom = $("#staff_nom");
var form_prenom = $("#staff_prenom");
$(form_nom).change(function() {
checkvariable(form_nom,form_prenom); //pass arguments into the function
});
$(form_prenom).change(function() {
checkvariable(form_nom,form_prenom);
});
function checkvariable(form_nom,form_prenom){ //innclude argument in function definition
//......
}
thank for your help. Yes I'm sure that the path is correct.
Here is my code from ../modules/verifier_staff.php
<?php
include('../inc/settings.php');
include('../inc/includes.php');
//$_POST['nom']='Doe';
//$_POST['prenom']='John';
$nom=$_POST['nom'];
$prenom=$_POST['prenom'];
$Dbcon = new Db;
$personne_manager = new PersonneManager($Dbcon->dbh);
$personne = $personne_manager->get(array('conditions'=> array('nom' => strtolower($nom),'prenom' => strtolower($prenom))));
if($personne)
{
$result = true;
}
else
{
$result = false;
}
return $result;
?>
this part works well (I tested it) so I think the problem come from my javascript code. I'm a newbie in javascript/jquery.

How does Google Script send an email when it detects changes in the background, like cron?

I have a Google spreadsheet with a script that checks a local library catalog to see if an ISBN-searched book has shown up or not. There's no API or RSS feature so I'm using UrlFetchApp to screen scrape for certain unique phrases that appear in the HTML source for a given book status.
Right now my spreadsheet only updates the "Status" column (which invokes the script based on an ISBN field) when I open the spreadsheet. Is there a way I can have Google Script run my main function and then trigger for changes? The documentation I've read has only mentioned user edits, which is not what I'm looking for.
function ISBNsearch(ISBN){
var consortURL = "http://LIBRARYURL/search~S6/?searchtype=i&searcharg=" + ISBN + "&searchscope=4";
var retrieveURL = UrlFetchApp.fetch(consortURL).getContentText();
var searchURLno = retrieveURL.search("BROWSE SCREEN TABLE");
var searchURLyes = retrieveURL.search("Item Location");
var searchURLordered = retrieveURL.search("copy ordered for");
if (searchURLno > -1){
var answer = "Not found"
} else if (searchURLyes > "-1") {
answer = "in Consort"
} else if (searchURLordered > "-1") {
answer = "on order"
} else if ((searchURLno == "-1") && (searchURLyes == "-1") && (searchURLordered == "-1")) {
answer = "no input"; }
return answer;
}
function testScript(){
var response = ISBNsearch("9782711802036");
Logger.log("The answer is " + response);
return response;
}
You can use Time Driven triggers to schedule function executions.
You can also programmatically manage these triggers.

Categories

Resources