How to update on node js using sqlite3? - javascript

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!

Related

Data being duplicated when the update function is triggered - Firebase

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

Vue update data within mongoose database and using axios call

I have a problem, I used a get and post function to retrieve and save some url in my database, but now I d like to update a variable count, that should rapresent the votes that every video get, and after that I shoul be able to disable the button that a user click to vote. So I'm having some troubles to make the update function, or I should use another post? But if so I probably create another element inside my DB or not?
so here there is my video and for now I set the video's id invisible using css, to test it and try to find the video with that specific id and make the update
<p class="invisible" id="idVideo"> {{item._id}} </p>
<iframe class="partecipant" v-bind:src="item.video.url"> </iframe>
<p id="voti" > {{item.voti.count}} </p>
<input type="button" id="buttonVoti" v-on:click="addVoto">
so here, when the user click the button with id= buttonVoti the v-on click call addVoto function
methods: {
...
//ALL THE OTHERS METHODS
...
...
...
//AND THEN THE ADDVOTO FUNCTION
addVoto : function () {
var self = this;
//self.videos[1].voti.count++
//console.log(self.videos._id);
var i = document.getElementById("idVideo");
var idVid =i.innerHTML;
console.log(idVid);
so here I can change the variable count, using self....count++ but I have to store and then retrieve again the same video with the new count updated.
here there is my model so the logic to access to the count should be this one
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var videoSchema = new Schema({
video : {
id : String,
url : String,
idchallenge : String
},
voti : {
count : {}
}
});
module.exports = mongoose.model('Video', videoSchema);
yes so I have a method called load video, that is activated when the user click a button called loadVideo
loadVideo : function (){
var linkYoutube = this.text;
console.log(linkYoutube);
//POST
axios.post('/video',{
method: 'post',
video: {
id: '1',
url: linkYoutube
},
voti: {
count: 0
}
});
and this is my get function,
getVideo: function () {
var self = this;
// Make a request for a user with a given ID
axios.get('/video')
.then(function (response) {
self.videos = response.data;
console.log(self.videos);
When you do you GET request to your video route, in your route logic, you should be able to use Mongoose count. Here is what that route might look like:
var Router = require('express').Router();
var mongoose = require('mongoose');
var Video = mongoose.model('Video');
Router.get('/videos', function(req, res) {
var response = {};
Video.find({}, function(queryErr, videos) {
if (!queryErr) {
response.videos = videos;
Video.count({}, function(countErr, count) {
if (!countErr) {
response.count = count;
res.status(200).send(response);
} else {
res.status(500).send(countErr);
}
});
} else {
res.status(500).send(queryErr);
}
});
});
module.exports = Router;
Here is a question about Mongoose count on Stack Overflow.

How to insert data in ground.db in meteor when offline

i have a fully working meteor application but now i want to make it offline so I installed ground:db and appcache here is my package file:
...
ground:db
appcache
ground:localstorage
then I changed my Collections to this:
Gebiete = new Mongo.Collection('gebiete');
Straßen = new Mongo.Collection('straßen');
Nummern = new Mongo.Collection('nummern');
Ground.Collection(Gebiete);
Ground.Collection(Straßen);
Ground.Collection(Nummern);
and now when the app is online i can insert data and then i disconnect the app and relaunch (cordova) and no data is lost.
But when im offline and i want to insert sth. it doesnt work ;(. I thaught i dont have to change my methods file but here is one method just to make sure if it is right:
Meteor.methods({
neuesGebiet(Besitzer, Gebietsname, Gebietsnummer, Ort, Erstellungsdatum) {
Gebiete.insert({
Besitzer: Besitzer,
Gebietsname: Gebietsname,
Gebietsnummer: Gebietsnummer,
Ort: Ort,
Erstellungsdatum: Erstellungsdatum
});
}
});
and on the client the message is called like this:
import { Meteor } from 'meteor/meteor'
Template.neuesGebietErstellen.onCreated(function () {
this.subscribe('gebiete');
});
Template.neuesGebietErstellen.events({
"submit .add-Gebiet": function (event) {
var Gebietsname = event.target.Gebietsname.value;
var Gebietsnummer = event.target.Gebietsnummer.value;
var Ort = event.target.Ort.value;
var Besitzer = Meteor.userId();
var Erstellungsdatum = new Date();
var Datum = Erstellungsdatum.toLocaleDateString();
Meteor.call('neuesGebiet', Besitzer, Gebietsname, Gebietsnummer, Ort, Datum)
FlowRouter.go('/');
return false;
}
});
Please help me to get the data inserted when offline because i want it to be 100% offline
Thank you ;)
It's been a while since I used Ground:db but here's what I think you are missing...
First, you probably only want grounded collections on Cordova, so
if(Meteor.isCordova) {
Ground.Collection(Gebiete);
Ground.Collection(Straßen);
Ground.Collection(Nummern);
}
Then you need to use Groundmethods to store your method calls. So after the definition of the method:
Meteor.methods({
'neuesGebiet': function(...) {
...
}
});
if( Meteor.isClient ) {
Ground.methodResume([
'neuesGebiet'
]);
}

obtain "server side" variable for use in html/javascript function

I'm trying to develop a language web application. It will scrape data from various websites and ask the participant numerous questions etc. I have created the file that scrapes the web page but I'm having difficulty getting the scraped variables from the node.js file. below are some extracts from the node js file:
var pword = function() {
var request = require("request");
var cheerio = require("cheerio");
var aa = Math.floor(Math.random() * 588);
var words = ['abash', 'aberrant', .....]
var A = words[aa];
var urlcollinsdictionary = "http://www.collinsdictionary.com/dictionary/english/";
var Newurldictionary = urlcollinsdictionary + A;
request({
uri: Newurldictionary,
}, function(error, response, body) {
var $ = cheerio.load(body);
$('div.homograph-entry').each(function() {
var link = $(this);
var text1 = link.text();
console.log(A);
console.log(text1);
});
});
}
My node js code works fine. my problem arises when I try to use its "text1" variable or others of its kind in my HTML/javascript coding. I've tried "getelementbyid", "variable exports/imports", even the "%%" method. but still no luck. as you've probably get guess I'm new to programming. please please please help me by making alterations/additions to the HTML code beneath to enable access to the text1 variable and others like it
<html>
<head>
language game
<title>language game</title>
<br>
<input type = "button" onclick = "word()" value = "Professional Vocab">
</head>
<body>
<script>
function word(){
alert(text1);
}
</script>
</body>
</html>
Thanks in advance...
You can add similar code in your node js file to respond to api calls
var express = require("express");
var app = express();
app.get("sendVar", function(request, response){
response.send(text1);
}
you will need to do npm install express in your command line
In your front-end side you can use jquery and make an AJAX call inside function word()
function word(){
$.get("url/sendVar", function(data){
alert(data);
});
}

CasperJS; opening dynamic url

I am using casperJS to got ta a page and collect an id from the URL, this is what my start function is doing. I would then like to put the id in to a new url and go there.
This works for the user var, this is coming in from the terminal. However sid is undefined and not the value I updated it to.
Any help would be appreciated. I am wondering if I found a limitation with casperJS. please not that this is not the full code (as that is much longer), if you message me I can provide you the whole script.
var user = decodeURIComponent(casper.cli.get(0).replace(/\+/g, ' '))
var sid
casper.start('https://editor.storify.com/', function() {
sid = this.getCurrentUrl()
sid = sid.split('/')[3]
})
casper.thenOpen('https://storify.com/' + sid, function() {
console.log('lets view the posts')
console.log(token)
console.log(sid)
if (dev)
this.capture('storeheading.png')
})
you can solve this problem by warping the thenOpen inside of a then
casper.then(function() {
casper.thenOpen('https://storify.com/' + sid, function() {
console.log('lets view the posts')
console.log(token)
console.log(sid)
if (dev)
this.capture('storeheading.png')
})
})

Categories

Resources