get the uuid in an object array with Firebase - javascript

Hello I would like to know if there would be a method to recover the uuid when I push data in my table I put you in the screenshot below
the code
push(ref(db, `/users/${auth.currentUser.uid}/user/sensors`), {
name: registerInformation.nameBox,
id: registerInformation.idBox,
categories: registerInformation.categories,
routine: registerInformation.routine,
});

The push function returns a Reference object, from which you can get the key with something like this:
const newRef = push(ref(db, `/users/${auth.currentUser.uid}/user/sensors`), {
name: registerInformation.nameBox,
id: registerInformation.idBox,
categories: registerInformation.categories,
routine: registerInformation.routine,
});
console.log(newRef.key);
If you want to use that key in the write operation, you can also separate the creating of the new ID from the writing of the data like this:
const newRef = push(ref(db, `/users/${auth.currentUser.uid}/user/sensors`));
console.log(newRef.key);
set(newRef, {
name: registerInformation.nameBox,
id: registerInformation.idBox,
categories: registerInformation.categories,
routine: registerInformation.routine,
});
In this snippet, the first line is a pure client-side operation that doesn't actually write anything to the database yet.

Related

replace json key with another json value using node js / javascript

testDetails.map(function(data){
let JsonData={
title: data["title"],
url: data["url"],
firstName: data["username"],
work:[{workid:data["workid"].slice(","), workname: "tester"}],
employee: [{employee_id: data["employee_id"].slice(","), employee_desk:"custom"}]
}
})
so my above json output look like this
{
"title":"01"
"url":"employe#url.com",
"firstName":"employee name",
"work":[{"workid":"9371", "workname": "tester"}],
"employee":[{"employee_id":"weh34",employee_desk:"custom"}]
}
and another JSON object where data look like this
let data={
"031w":"ewid3728e",
"9371":"emp_01",
"weh34":"work_place01"
}
The workid and employee id with the JsonData I want to replace with the value which is present inside the data json so my data which is look like this
"work":[{"workid":"9371", "workname": "tester"}],
"employee":[{"employee_id":"weh34",employee_desk:"custom"}]
needed to be look like this
"work":[{"workid":"work_place01", "workname": "tester"}],
"employee":[{"employee_id":"emp_01",employee_desk:"custom"}]
I am trying to replace the code manually like this
work:[{workid:data.weh34, workname: "tester"}],
employee: [{employee_id: data.9371, employee_desk:"custom"}]
but data["workid"].slice(",") and data["employee_id"].slice(",") always get changed so I want to match both the json and fetch the value and place it in my JsonData
Just use the value you got from testDetails as the index in the data object to get the corresponding value.
You need to use a different variable from data as the parameter to the mapping function so you can refer to the external variable data. I changed it to d below.
testDetails.map(d => ({
title: d["title"],
url: d["url"],
firstName: d["username"],
work: [{
workid: data[d["workid"].slice(",")],
workname: "tester"
}],
employee: [{
employee_id: data[d["employee_id"].slice(",")],
employee_desk: "custom"
}]
}));

Edit readonly javascript objects

I have a read-only object that is returned by GraphQL (vue-apollo) query, the result which is read-only looks something like this:
result: {
id: 'yh383hjjf',
regulations: [{ title: 'Test', approved: false}]
})
I want to bind this to a form and be able to edit/update the values in the regulations array and save it back to the database.
at the moment when I try to edit I get the error below:
Uncaught TypeError: "title" is read-only
I tried cloning the result returned by the database using object.assign
//target template
const regulatoryApprovals = {
id: null,
regulations: [{ title: null, approved: null}]
})
regulatoryApprovals = Object.assign(regulatoryApprovals, result, {
regulations: Object.assign(regulatoryApprovals.regulations, result.regulations)
})
but this didn't work.
Does anyone know how I can properly clone the result?
regulatoryApprovals= Object.assign(regulatoryApprovals, ... indicates the problem because regulatoryApprovals is modified with Object.assign, so it would need no assignment.
Read-only regulatoryApprovals object needs to be cloned. regulations is an array and won't be merged correctly with Object.assign, unless it's known that array elements need to be replaced. It should be:
regulatoryApprovals = {
...regulatoryApprovals,
...result,
regulations: [...regulatoryApprovals.regulations, result.regulations]
}
Where { ...regulatoryApprovals, ... } is a shortcut for Object.assign({}, regulatoryApprovals, ...).

Trying to set a value by referencing separate json

Im trying to separate out the functionality of my model and the data so ive created a separate json file with a basic table
when my model builds it creates an object and i need it to create a value in it based on a value coming in:
{
"1":"apple",
"2":"banana",
"3":"orange",
"4":"grape"
}
async save (xmlOrder) {
let customerOrder = {
ID: xmlOrder.ID,
Name: xmlOrder.Name ,
ItemCode: xmlOrder.ItemCode ,
Fruit: (This set by referencing the json, based on the Item code coming in above)enter code here
}
You can import that json object in file where you're having your model, than based on input to function you can get value out of object.
let obj = {"1":"apple","2":"banana","3":"orange","4":"grape"}
function save (xmlOrder) {
let customerOrder = {
ID: xmlOrder.ID,
Name: xmlOrder.Name ,
ItemCode: xmlOrder.ItemCode ,
Fruit: obj[xmlOrder.ItemCode] || 'Not in list',
}
return customerOrder
}
console.log(save({ID:33,Name:'Name',ItemCode:'2'}))
console.log(save({ID:303,Name:'Name1',ItemCode:'21'}))

Javascript reset object inside array

I have the below JS code in my Ember app that gets called;
myPanels.accordionPanels = [];
myPanels.accordionPanels.push({
panel: {
name: "my-grid",
type: 'comp',
props: [{
key: 'elementId',
value: "myCustomId"
}]
}
});
So as you can see, I start by setting myPanels.accordionPanels = [] every time and then push the object.
However, I got the following error
Assertion Failed: Attempted to register a view with an id already in
use: myCustomId
So I am assuming that the object inside is not getting reset & it is able to find the earlier created "myCustomId".
Am I resetting the array (or rather the object inside it) correctly ?
Since I am able to push values using:
accordionPanels = [];
accordionPanels.push({
panel: {
name: "my-grid",
type: 'comp',
props: [{
key: 'elementId',
value: "myCustomId"
}]
}
});
make sure myPanels.accordionPanels doesn't have any prototype associated with it.
Try to inspect its value as:
myPanels.accordionPanels = [];
console.log(myPanels.accordionPanels); // see if it has values.
You can delete value using :
delete myPanels.accordionPanels PROTOTYPE

ExtJS creating record from existing store

I have created an ext store like so:
var store = new Ext.data.JsonStore({
root: 'vars',
fields: [{ name: 'rec_id', mapping: 'rec' }, { name: 'identity', mapping: 'id'}]
});
This works alright when I add data to the store via loadData(); and some json which looks like:
{ vars : {rec: '1', id:'John'} }
My problem is that if I use add(); to get this record into the store I have to first create it as an Ext.data.Record object.
I do this as pointed out here: https://stackoverflow.com/a/7828701/1749630 and it works ok.
The issue I have is that the records are entered with their mapped parameters rather than the ones I set. I.e, 'rec_id' becomes 'rec' and 'identity' becomes 'id'.
What am I doing wrong here?
You need to do the mapping manually, something like this:
var myNewRecord = new store.recordType({
rec_id: vars.rec,
identity: vars.id
});
store.add(myNewRecord);

Categories