firebase cloud function ServerValue increment not working - node.js

i have a firebase function. I want to make increment ServerValue here. but it doesn't work it gives an error. where did i go wrong?
I am attaching the console screenshot below. (note : I don't know javascript very well, there may be errors in java codes as well.)
const functions = require("firebase-functions");
const admin = require("firebase-admin");
admin.initializeApp(functions.config().firebase);
const database = admin.database();
async function sirala(dil, lig, oyunTipi) {
await database.ref("/turnuvalar/" + dil + "/" + lig + "/" + oyunTipi + "/users/").once("value").then(async function(snap) {
const users = [];
const updatesLig = {};
const refLig = database.ref().child("ligler").child(dil).child(lig).child("users");
snap.forEach((childSnap) => {
const id = childSnap.key;
const puan = childSnap.child("puan").val();
const serverTime = childSnap.child("serverTime").val();
users.push(new Person(id, puan, serverTime));
});
users.sort((a, b) => parseFloat(a.serverTime) - parseFloat(b.serverTime));
for (let i = 0; i < users.length; i++) {
let group = [];
const grupSayi = 3;
if (i < grupSayi) {
group = users.slice(0, grupSayi);
} else {
group = users.slice(i + 1 - grupSayi, i + 1);
}
group.sort((a, b) => parseFloat(b.puan) - parseFloat(a.puan));
const index = (element) => element.id === users[i].id;
const sira = (group.findIndex(index) + 1);
// const increment = database.ServerValue.increment(kupaAdet(sira));
const kupa = {
puan: database.ServerValue.increment(1), /// ??? ERROR IS THERE ???
};
updatesLig[users[i].id + "/"] = kupa;
console.log("user id " + users[i].id + " kupa - " + kupaAdet(sira) + " sira - " + sira);
for (let g = 0; g < group.length; g++) {
console.log("grup : " + (i + 1) + " - " + group[g].id + " - " + group[g].puan + " - " + group[g].serverTime);
}
}
await refLig.update(updatesLig);
return null;
}).catch((error) => {
console.log(error);
});
}
error screenshot :

It looks like your database is an instance of firebase.database() (note the parentheses in there). The ServerValue property is defined on firebase.database without parentheses, so that explains why the increment cannot be found.
You'll want to use either admin.database.ServerValue.increment(1) or firebase.database.ServerValue.increment(1), depending on how you import the Admin SDK.

Related

Edit a JSON object

I retrieved a JSON object from a local database, I want to edit a value (invItems) and add a new value to it (filed[filed.invItems]), then upload it back to the database, but it does not seem to work (the JSON does not seem to change)
async function invPut(itemID, message) {
var filed = await frenzyDB.getKey(id + "_invcache");
console.log("Before: " + filed)
newInvItems = filed.invItems + 1;
filed.invItems = newInvItems;
filed[filed.invItems] = itemID;
console.log("After: " + filed);
await frenzyDB.addKey(id + "_invcache", filed)
}
Console Output:
Before: {"invItems":0}
After: {"invItems":0}
It shows no errors, but the JSON doesnt change. Am I doing something wrong? If so, what can I do to fix it?
Thanks for all your help!
Notes:
frenzyDB is just a javascript file that deals with a standard REPL.it Database
Code of frenzyDB:
const Database = require("#replit/database")
const db = new Database()
async function addKey(key, value) {
await db.set(key, value).then(() => {return;});
}
async function getKey(key) {
return await db.get(key).then(value => {return value;});
}
function listAllKeys() {
db.list().then(keys => {return keys;});
}
async function hasKey(key) {
var keys = await listAllKeys();
if (keys.includes(key)) {
return true;
} else {
return false;
}
}
async function removeKey(key) {
await db.delete(key).then(() => {return;});
}
module.exports = {
addKey,
getKey,
listAllKeys,
hasKey,
removeKey
};
Edit: Latest code:
async function invPut(itemID, message) {
await init(message.author.id);
var filed = await frenzyDB.getKey(message.author.id + "_invcache");
console.log(filed)
const result = {};
result.invItems = (filed['invItems'] + 1) || 1;
result.hasOwnProperty(filed.invItems) ? result[filed.invItems + 1] = itemID : result[filed.invItems] = itemID;
console.log(result);
frenzyDB.addKey(message.author.id + "_invcache", result)
message.reply("A **"+ itemIDs[itemID].name + "** was placed in your inventory");
return true;
}
EDIT 2: Latest Console Output:
{ '4': 3, invItems: 5 }
{ '5': 3, invItems: 6 }
Any help will be appreciated!
Thanks
Try this
// Demo Data
const itemID = 10;
var filed = { "invItems" : 0 };
// Real function
console.log("Before: " + JSON.stringify(filed));
const result = {};
result.invItems = (filed['invItems'] + 1) || 1;
result.hasOwnProperty(filed.invItems) ? result[filed.invItems + 1] = itemID : result[filed.invItems] = itemID;
console.log("After: " + JSON.stringify(result));
The result I get is
Before: {"invItems":0}
After: {"0":10,"invItems":1}
You would then of course use result to store the data away in the DB.
async function invPut(itemID, message) {
// Typo?
var filed = await frenzyDB.getKey(itemID + "_invcache");
console.log("Before: " + filed)
const result = {};
result.invItems = (filed['invItems'] + 1) || 1;
result.hasOwnProperty(filed.invItems) ? result[filed.invItems + 1] = itemID : result[filed.invItems] = itemID;
console.log("After: " + result);
// Typo?
await frenzyDB.addKey(itemID + "_invcache", result)
}
Answer Edit:
const result = { ...filed };
result.invItems = (filed['invItems'] + 1) || 1;
result.hasOwnProperty(filed.invItems) ? result[filed.invItems + 1] = itemID : result[filed.invItems] = itemID;
console.log(JSON.stringify(result));
maybe this will help you
const json = fs.readFileSync(`${__dirname}/data/data.json`, "utf-8");
const inputData = JSON.parse(json);
inputData.push({input: 'front'}) // creates new element for data.json
-------------------------------------------
array.push({front: 'front', back: 'back'});

I am unable to read multiple rows from xlsx file

I am trying to read multiple rows from xlsx file. My xlsx sheet contains various details one of which is an FTP directory, password detail.
Each row contains a different FTP directory.
I am able to read, fetch and retrieve the data from FTP if only one row is mentioned in the sheet
but as soon as I add an extra row to be read it starts showing promise error,
Below is my code
dataJson1 is the excel sheet(JSON)
for (let i = 0; i < dataJson1.length; i++) {
dir = dataJson1[i]['FTP DIR'];
subfolder = dataJson1[i]['Sub Folder'];
ftpPath = dir + subfolder;
host = dataJson1[i]['FTP IP'];
user = dataJson1[i]['FTP Username'];
password = dataJson1[i]['FTP Password'];
ticketStatus = dataJson1[i]['Status']
console.log("FTP LOCATION:" + ftpPath + "\n" + "HOSTNAME:" + host + "\n" + "USERNAME:" + user + "\n" + "PASSWORD:" + password + "\n")
//Reading the list of directories present in the FTP location
console.log("value of i" + i);
///////////
if (ticketStatus == true) {
if (!ftp) {
ftp = new PromiseFtp();
}
const ftpPromise = ftp.connect({
host: host, user: user,
password: password
}).then(function (serverMessage) {
console.log('Server message: ' + serverMessage)
//console.log("value of i" + i)
return ftp.list(ftpPath);
}).then(function (list) {
console.log('Directory listing:');
var dirList = (list)
console.log(dirList)
console.log("Number of directories:" + dirList.length)
var jsondirString = JSON.stringify(dirList)
var datadirJson1 = JSON.parse(jsondirString)
for (var j = 0; j < dirList.length; j++) {
//console.log(datadirJson1[j]['name'])
ticketName.push(datadirJson1[j]['name'])
//console.log(ftpTime)
ftpTime.push(datadirJson1[j]['date'])
}
return ftp.end();
});
promises.push(ftpPromise)
}//status check ends
}//Loop ends here
Promise.all(promises).then(arrayOfResults => {
console.log(ticketName);
var ticketNameArr = [];
for (let i = 0; i < ticketName.length; i++) {
let ticketNameIs = ticketName[i];
let ftpTimeIs = ftpTime[i]
let ticketDetail = ticketApp(ticketNameIs, ftpTimeIs);
Promise.all(ticketDetail).then(function (values) {
//console.log(values);
ticketNameArr.push({
// FtpTime: values[0].ftpTime,
Date: values[0].ftpTime,
TicketName: ticketNameIs,
//Add Dynamic folder column----
In_Input_Location: values[0].ticketStatusInput,
Input_Time: values[0].mtime,
In_Result_Location: values[1].ticketStatusResult,
Result_Time: values[1].mtime,
//Will help in preparing comments
CallAPi: values[3].apiStatus,
ReportStatus: values[3].reportStatus,
Comment: values[4].Comment,
Status: values[5].ticketStatus
// LogStatus: values[2].logStatus,
// LogCreateTime: values[2].birthtime,
// LogModifiedTime: values[2].mtime,
});
if (ticketNameArr.length == ticketName.length) {
//uncomment below command if sheet is blank then comment back
// ws = XLSX.utils.sheet_add_json(ws, ticketNameArr,{origin:0, skipHeader:false});
//comment below command if sheet is blank then uncomment
ws = XLSX.utils.sheet_add_json(ws, ticketNameArr, { origin: -1, skipHeader: true });
//
let wsRemDup = removeDuplicate(ws)
console.log("Unique Data", wsRemDup)
//OverWriting Unique data
wb.Sheets[first_sheet_name] = XLSX.utils.json_to_sheet(wsRemDup);
XLSX.writeFile(wb, 'DailyTicketSatus.xlsx')
// respond.render('result', { "ticketNameArr": ticketNameArr });
respond.render('index', { "ticketNameArr": ticketNameArr });
}
});
}
})
})```
I would suggest using the async/await syntax for this task, it's easier to read and you can ensure that the ftp.end() call is complete before proceeding to the next ftp host. This is probably the reason why the original code is failing to process more than one row.
async function runFTPJob(dataJson1) {
let promises = [];
for (let i = 0; i < dataJson1.length; i++) {
dir = dataJson1[i]['FTP DIR'];
subfolder = dataJson1[i]['Sub Folder'];
ftpPath = dir + subfolder;
host = dataJson1[i]['FTP IP'];
user = dataJson1[i]['FTP Username'];
password = dataJson1[i]['FTP Password'];
ticketStatus = dataJson1[i]['Status']
console.log("Getting row:", i);
console.log("FTP LOCATION:" + ftpPath + "\n" + "HOSTNAME:" + host + "\n" + "USERNAME:" + user + "\n" + "PASSWORD:" + password + "\n")
//Reading the list of directories present in the FTP location
if (ticketStatus == true) {
if (!ftp) {
ftp = new PromiseFtp();
}
try {
let ftpPromise = ftp.connect({ host, user, password });
promises.push(ftpPromise);
let serverMessage = await ftpPromise;
console.log('Server message: ' + serverMessage)
let dirList = await ftp.list(ftpPath);
console.log('Directory listing:', dirList);
console.log("Number of directories:" + dirList.length)
for (let dirEntry of dirList) {
ticketName.push(dirEntry.name);
ftpTime.push(dirEntry.date);
}
await ftp.end();
} catch (e) {
console.error("An error occurred accessing ftp site:", e.message);
}
}//status check ends
}//Loop ends here``
return promises;
}
function processTickets() {
console.log(ticketName);
var ticketNameArr = [];
for (let i = 0; i < ticketName.length; i++) {
let ticketNameIs = ticketName[i];
let ftpTimeIs = ftpTime[i]
let ticketDetail = ticketApp(ticketNameIs, ftpTimeIs);
Promise.all(ticketDetail).then(function (values) {
//console.log(values);
ticketNameArr.push({
// FtpTime: values[0].ftpTime,
Date: values[0].ftpTime,
TicketName: ticketNameIs,
//Add Dynamic folder column----
In_Input_Location: values[0].ticketStatusInput,
Input_Time: values[0].mtime,
In_Result_Location: values[1].ticketStatusResult,
Result_Time: values[1].mtime,
//Will help in preparing comments
CallAPi: values[3].apiStatus,
ReportStatus: values[3].reportStatus,
Comment: values[4].Comment,
Status: values[5].ticketStatus
// LogStatus: values[2].logStatus,
// LogCreateTime: values[2].birthtime,
// LogModifiedTime: values[2].mtime,
});
if (ticketNameArr.length == ticketName.length) {
//uncomment below command if sheet is blank then comment back
// ws = XLSX.utils.sheet_add_json(ws, ticketNameArr,{origin:0, skipHeader:false});
//comment below command if sheet is blank then uncomment
ws = XLSX.utils.sheet_add_json(ws, ticketNameArr, { origin: -1, skipHeader: true });
//
let wsRemDup = removeDuplicate(ws)
console.log("Unique Data", wsRemDup)
//OverWriting Unique data
wb.Sheets[first_sheet_name] = XLSX.utils.json_to_sheet(wsRemDup);
XLSX.writeFile(wb, 'DailyTicketSatus.xlsx')
// respond.render('result', { "ticketNameArr": ticketNameArr });
respond.render('index', { "ticketNameArr": ticketNameArr });
}
});
}
}
( async() => {
await runFTPJob(dataJson1);
processTickets();
})();

Problem with findOne() in sequelize node.js

I have a problem with node.js and sequelize findOne(). I want to find new students, that I want to add to the DB (var novi), and the ones that already exist, I just want to update their field (var stari). Everything works as expected, only when I want to return JSON with how many new students I added to the DB, and how many are updated, values of stari and novi, go back to 0, but the counting is good, I checked. I know the problem is with asynchronous call, but I don't know how to fix.
app.post('/student', function(req,res) {
var imeGodine = req.body['godina'];
//POMOĆNE SKRIPTE BitBucket.js i citanjeGodina.js
var broj = 0;
var stari = 0;
var novi = 0;
db.godina.findOne({where:{nazivGod:req.body.godina}}).then(god => {
var studenti = req.body.studenti;
db.student.count().then (ranijeStudenata => {
for(var i = 0; i<studenti.length; i++) {
var ime = studenti[i].imePrezime;
var ind = studenti[i].index;
db.student.findOne({where:{index :studenti[i].index}}).then(stud => {
if (stud == null) {
novi++;
db.student.create({imePrezime:ime, index : ind}).then(noviStudent => {
god.addStudenti(noviStudent);
});
}
else if (stud != null) {
stari++;
god.addStudenti(stud);
}
});
broj++;
}
var brojNovih = broj - ranijeStudenata; //ne koristi se, ali možda hoće
res.set("Content-Type", "application/json");
res.status(200).send(JSON.stringify({message: "Dodano je " + novi + " novih studenata i upisano " + stari + " na godinu " + imeGodine}));
});
});
});
Picture of code
You can use async/await to do counting in a synchronous way.
'use strict';
app.post('/student', async function (req, res) {
var imeGodine = req.body['godina'];
var {studenti} = req.body;
var broj = 0;
var stari = 0;
var novi = 0;
let god = await db.godina.findOne({where: {nazivGod: req.body.godina}});
let ranijeStudenata = await db.student.count(); // ranijeStudenata not used?
for (var i = 0; i < studenti.length; i++) {
var ime = studenti[i].imePrezime;
var ind = studenti[i].index;
let stud = await db.student.findOne({where: {index: studenti[i].index}});
if (stud === null) {
novi++;
let noviStudent = await db.student.create({imePrezime: ime, index: ind});
god.addStudenti(noviStudent);
} else if (stud !== null) {
stari++;
god.addStudenti(stud);
}
broj++;
}
return res.status(200).send({
message: "Dodano je " + novi + " novih studenata i upisano " + stari + " na godinu " + imeGodine
});
});

Making an async loop in node.js with sql

Here is what I'm trying to do:
Start a looping (10x)
Select on sql to return 1 register (select top 1 where 'running' is null)
Sql update 'running' status to 'running'
If the record is null, I access an API and get some data
The result is updated on the initial sql record (set running = 'ok')
End looping (start over)
Thing is, node.js does not wait for start over, it does everything at the same time. That way, 'running' is always null.
var express = require('express');
var app = express();
var c_MyApi = require('./controller/call_MyApi');
var mongo = require('./controller/crud_mongo');
var c_email = require('./controller/api_email_verify');
var c_sql = require('./controller/consulta_sql');
var MyLink = '',
id = 0;
for( var i = 0 ; i < 10 ; i++){
c_sql.busca().then(function(res) {
MyLink = res[0].MyLink;
id = res[0].id;
c_sql.marca(id).then(
c_MyApi.busca(MyLink, function(a) {
if (a == 0) {
c_sql.atualiza(id, 'sem_email', 's/e');
}
if (a == 1) {
c_sql.atualiza(id, 'link_errado', 'l/e');
} else {
for (var i = 0; i < a.length; i++) {
var email = a[i].address;
c_email.busca(email, function(e_valid) {
c_sql.atualiza(id, email, e_valid)
})
}
}
})
)
})
}
}
//consulta_sql.js
var sql = require("seriate");
var search = function() {
var exec = 'select top 1 MyLink, id from sys_robo_fila where done is null';
sql.setDefaultConfig(config);
return sql.execute({
query: exec
});
}
var usado = function(id) {
var exec = "update sys_robo_fila set done = 'r' where id = " + id + "";
sql.setDefaultConfig(config);
return sql.execute({
query: exec
});
}
var update = function(id, email, valid) {
var exec = "update sys_robo_fila set email = '" + email + "' , validacao = '" + valid + "' , done = 'ok' where id = " + id + "";
sql.setDefaultConfig(config);
return sql.execute({
query: exec
});
}
module.exports = {
busca: search,
atualiza: update,
marca: usado
}
Any sugestions?
The call to c_sql.busca() is returning a Promise immediately and then continuing on to the next loop before then() is called, which is why they seem to run at the same time (they are actually just running very quickly, but before the Promise is resolved.
If you want this to run synchronously, one loop at a time, I would recommend using a recursive function to not start the loop again until the Promises have resolved.
let count = 0;
function doSomething() {
// this returns immediately, before .then() executes
return c_sql.busca()
.then(() => {
// do some more stuff after c_sql.busca() resolves
return c_sql.busca();
})
.then(() => {
// increment your count
count += 1
if (count<10) {
// less than 10 runs call the function again to start over
return doSomething();
}
});
}
This article might be helpful in understanding Promises: https://medium.com/#bluepnume/learn-about-promises-before-you-start-using-async-await-eb148164a9c8

Mongojs find by uuid _id

I am trying to find one record with "monogjs" by _id.
Our _id is a guid (.net).
So I have something like this "80cd95b8-79bf-4025-933b-cabc71fbdc9f" as a string.
Now I tried "monogdb.bsonpure" with a "buffer" specifying the subtype of uuid.
I tried passing it to objectid() but then it tells me it need to be a hex string 12/24.
I just passed it as string but then it just does not return anything
Ok this works
Take "80cd95b8-79bf-4025-933b-cabc71fbdc9f" remove "-"
var Binary = require('mongodb').Binary;
var uuid = require('node-uuid');
var base64data = new Buffer(uuid.parse(param), 'binary').toString('base64');
var bin = new Buffer(base64data, 'base64');
var id = new Binary(bin, Binary.SUBTYPE_UUID_OLD);
So the accepted answer didn't work for me. I found a snippet of code in this github issue to parse a .net guid into a buffer:
guid-parse.js:
'use strict';
// Maps for number <-> hex string conversion
var _byteToHex = [];
var _hexToByte = {};
for (var i = 0; i < 256; i++) {
_byteToHex[i] = (i + 0x100).toString(16).substr(1);
_hexToByte[_byteToHex[i]] = i;
}
// **`parse()` - Parse a UUID into it's component bytes**
function parse(s, buf, offset) {
const i = (buf && offset) || 0;
offset = i;
let ii = 0;
buf = buf || Buffer.alloc(16 + i);
s.toLowerCase().replace(/[0-9a-f]{2}/g, function(oct) {
if (ii < 16) { // Don't overflow!
buf[i + ii++] = _hexToByte[oct];
}
});
// Zero out remaining bytes if string was short
while (ii < 16) {
buf[i + ii++] = 0;
}
// Endian-swap hack...
var buf2 = Buffer.from(buf);
buf[offset+0] = buf2[offset+3];
buf[offset+1] = buf2[offset+2];
buf[offset+2] = buf2[offset+1];
buf[offset+3] = buf2[offset+0];
buf[offset+4] = buf2[offset+5];
buf[offset+5] = buf2[offset+4];
buf[offset+6] = buf2[offset+7];
buf[offset+7] = buf2[offset+6];
return buf;
}
// **`unparse()` - Convert UUID byte array (ala parse()) into a string**
function unparse(buf, offset) {
let i = offset || 0;
// Endian-swap hack...
var buf2 = Buffer.from(buf);
buf[i+0] = buf2[i+3];
buf[i+1] = buf2[i+2];
buf[i+2] = buf2[i+1];
buf[i+3] = buf2[i+0];
buf[i+4] = buf2[i+5];
buf[i+5] = buf2[i+4];
buf[i+6] = buf2[i+7];
buf[i+7] = buf2[i+6];
const bth = _byteToHex;
return bth[buf[i++]] + bth[buf[i++]] +
bth[buf[i++]] + bth[buf[i++]] + '-' +
bth[buf[i++]] + bth[buf[i++]] + '-' +
bth[buf[i++]] + bth[buf[i++]] + '-' +
bth[buf[i++]] + bth[buf[i++]] + '-' +
bth[buf[i++]] + bth[buf[i++]] +
bth[buf[i++]] + bth[buf[i++]] +
bth[buf[i++]] + bth[buf[i++]];
}
module.exports = {
parse,
unparse
};
Then I used it like this:
const mc = require('mongodb').MongoClient;
const { Binary } = require('mongodb').Binary
const guidParse = require("./guid-parse.js");
const NUUID = guidString => {
return new Binary(guidParse.parse(guidString), Binary.SUBTYPE_UUID_OLD);
};
mc.connect('mongodb://localhost:27017/database').then( conn => {
const db = conn.db('database');
return db
.collection('users')
.find({
Guid: NUUID("9EC5955B-E443-456A-A520-8A87DED37EBB")
})
.toArray();
}).then( users => {
console.log(users);
});
And it returned the collection I was looking for!

Resources