retrieving data from firebase in react- redux app - reactjs

firebase rules
{
"rules": {
"ingredients": {
".read": true,
".write": true,
},
"orders": {
".read": "auth != null",
".write": "auth != null",
".indexOn": ["userId"]
}
}
}
firebase data
{
"ingredients" : {
"bacon" : 0,
"cheese" : 0,
"meat" : 0,
"salad" : 0
},
"orders" : {
"-M-gRBK60ImmEg55J2lC" : {
"ingredients" : {
"bacon" : 0,
"cheese" : 0,
"meat" : 1,
"salad" : 1
},
"orderData" : {
"country" : "a",
"deliveryMethod" : "fastest",
"email" : "a#a.com",
"name" : "a",
"street" : "aa",
"zipcode" : "11111"
},
"price" : 1.8,
"userId" : "LyKBp44aSgPiGPdKvClUNtddJ7G3"
}
}
}
here i want to retrieve orders from firebase and filter it according to userId
to display user specific orders
but it fails and console.log(res.data) give me empty obj { }
instead of the orders .. there's no error message
.. what is the problem here ??
export const fetchOrders = (token, userId) => {
return dispatch => {
dispatch(fetchOrdersStart()); // to set loading to true
const queryParams = '?auth=' + token + '&orderBy="userId"&equalTo="' + userId + '"';
axios.get( '/orders.json' + queryParams)
.then( res => {
console.log(res.data);
const fetchedOrders = [];
for ( let key in res.data ) {
fetchedOrders.push( {
...res.data[key],
id: key
} );
}
dispatch(fetchOrdersSuccess(fetchedOrders));
} )
.catch( err => {
dispatch(fetchOrdersFail(err));
} );
};
};

Related

DeleteOne by _id Mongodb

This is my first stack overflow question, and I'm excited to hear your thoughts.
> db.travelers.find().pretty()
{
"_id" : ObjectId("615e3ddece93c870aca4e465"),
"assembly" : [
{
"_id" : ObjectId("615e3ddece93c870aca4e466"),
"SN" : 24,
"Model" : "J240",
"Job" : 1234,
"Revision" : "A",
"Version" : 1
}
],
"assyHistory" : [ ],
"subAssemblies" : [
{
"_id" : ObjectId("615e3deace93c870aca4e467"),
"SN" : 19,
"Model" : "P500"
}
I am trying to delete the nested data in "subAssemblies" by its id. My current route:
router.delete("/subassembly/:id", deleteOneSubassembly);
my current controller:
deleteOneSubassembly: async (req, res) => {
try {
const removeOne = await Traveler.deleteOne({
_id: req.params.id
});
console.log(removeOne);
res.json(removeOne);
} catch (err) {
res.send({ error: err });
}
}
And all I keep getting back is:
"n": 0,
"nModified": 0,
"ok": 1
Wondering where I might be going wrong?
use update and $pull the item
const removeOne = await Traveler.updateOne(
{
"subAssemblies._id": req.params.id
},
{
"$pull": { subAssemblies: { _id: req.params.id }
}
});
aggregate
db.collection.update({
"subAssemblies._id": ObjectId("615e3deace93c870aca4e467")
},
{
$pull: {
subAssemblies: {
_id: ObjectId("615e3deace93c870aca4e467")
}
}
})
mongoplayground

setItem in AsyncStorage doesn't work

I'm trying to upload some data to the server from React Native. The data is stored in AsyncStorage.
The data is as follows :
let vehicles = [
{
"id": 1,
"make_text": "Peugeot",
"model_text": "208",
"color_text": "Argent",
"category_text": "CT",
"tag_text": "",
"vin": "123456",
"registration": "",
"reference": "",
"comment": "",
"autralis_id": 151390
},
{
"id": 1,
"make_text": "Peugeot",
"model_text": "307",
"color_text": "Bleu",
"category_text": "CT",
"tag_text": "",
"vin": "654321",
"registration": "",
"reference": "",
"comment": "",
"autralis_id": 156413
}
]
And
let vehicle_slots = [
{
"vehicle_id": 1,
"slot_id": 118,
"area": "",
"zone": "B",
"aisle": "",
"side": "S",
"col": 2,
"level": 0,
"position": 0,
"timestamp": "201705021544",
"picturepath": "",
"pictureguid": "0a016bb9-b7bb-4dd7-a0bf-407ef31a0c1a",
"reason": "ENTER",
"handled": 0,
"uploaded": 0
},
{
"vehicle_id": 1,
"slot_id": 2521,
"area": "",
"zone": "C",
"aisle": "",
"side": "E",
"col": 4,
"level": 0,
"position": 0,
"timestamp": "201705021544",
"picturepath": "",
"pictureguid": "64c726e2-37ec-4ab7-8b57-b08e9899086a",
"reason": "ENTER",
"handled": 0,
"uploaded": 0
}
]
I want to send only those which have uploaded and handled property equals to 0.
Thus, first I get those values where uploaded and handled property is equal to 0. I do it as follows:
try {
let vehicle_data = await AsyncStorage.getItem('vehicle');
if (vehicle_data !== null){
// We have data!!
let vehicle_slot_data = await AsyncStorage.getItem('vehicle_slot');
if (vehicle_slot_data !== null){
vehicle_data = JSON.parse(vehicle_data);
vehicle_slot_data = JSON.parse(vehicle_slot_data);
let result_to_handle = [];
let result_to_upload = [];
vehicle_slot_data.forEach(v => {
let not_handled = vehicle_data.find(m => m.id === v.vehicle_id && v.handled === 0);
let not_uploaded = vehicle_data.find(m => m.id === v.vehicle_id && v.uploaded === 0);
if ( not_uploaded ) result_to_upload.push(that.renderData(v, not_uploaded)[0]);
if ( not_handled ) result_to_handle.push(that.renderData(v, not_handled)[0]);
});
//Here I have result_to_handle and result_to_upload with the data that I need to send to the server.
............
If result_to_handle and result_to_upload have length > 0 then I want to send them to the server and update the property handled and property uploaded to 1 in AsyncStorage.
I try to do it as follows :
..............
if (result_to_handle.length > 0){
let options = {
method: 'POST',
body: JSON.stringify(result_to_handle),
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json'
}
};
fetch(data_url + "/manager/transport/sync/movements/", options)
.then(response => response.json())
.then(responseData => {
AsyncStorage.getItem('vehicle_slot')
.then(json => {
let data = [];
if (json) data = JSON.parse(json);
let result = [];
forEach(data, value => {
if( value.handled === 0 ){
value.handled = 1;
result.push(value);
}else{
result.push(value);
}
});
AsyncStorage.setItem('vehicle_slot', JSON.stringify(result))
});
});
}
if (result_to_upload.length > 0){
forEach(result_to_upload, value => {
if (value.picturepath) {
let body = new FormData();
const photo = {
uri: value.picturepath,
type: 'image/jpeg',
name: value.pictureguid + '.jpg',
};
body.append('image', photo);
let xhr = new XMLHttpRequest();
xhr.open('POST', data_url + "/manager/transport/sync/picture/?pictureguid=" + value.pictureguid);
xhr.onload = (e) => {
if (xhr.readyState === 4) {
if (xhr.status === 200) {
AsyncStorage.getItem('vehicle_slot')
.then(json => {
if (json){
let data = JSON.parse(json);
let result_data = [];
forEach(data, val => {
if( val.pictureguid === value.pictureguid ){
val.uploaded = 1;
result_data.push(val);
}else{
result_data.push(val);
}
});
AsyncStorage.setItem('vehicle_slot', JSON.stringify(result_data), () => s_cb())
}
});
}
}
};
xhr.onerror = (e) => console.log('Error');
xhr.send(body);
} else {
AsyncStorage.getItem('vehicle_slot')
.then(json => {
if (json){
let data = JSON.parse(json);
let result_data = [];
forEach(data, val => {
if( val.pictureguid === value.pictureguid ){
val.uploaded = 1;
result_data.push(val);
}else{
result_data.push(val);
}
});
AsyncStorage.setItem('vehicle_slot', JSON.stringify(result_data), () => s_cb())
}
});
}
});
}
But the result that I get is not correct.
The vehicle_slot from AsyncStorage is as follows:
[
{
"vehicle_id": 1,
"slot_id": 118,
"area": "",
"zone": "B",
"aisle": "",
"side": "S",
"col": 2,
"level": 0,
"position": 0,
"timestamp": "201705021544",
"picturepath": "",
"pictureguid": "0a016bb9-b7bb-4dd7-a0bf-407ef31a0c1a",
"reason": "ENTER",
"handled": 1,
"uploaded": 0
},
{
"vehicle_id": 1,
"slot_id": 2521,
"area": "",
"zone": "C",
"aisle": "",
"side": "E",
"col": 4,
"level": 0,
"position": 0,
"timestamp": "201705021544",
"picturepath": "",
"pictureguid": "64c726e2-37ec-4ab7-8b57-b08e9899086a",
"reason": "ENTER",
"handled": 1,
"uploaded": 1
}
]
Thus, for the first object is the value uploaded still equal to 0 but it should be equal to 1.
Any advice?

How to display the output in Tree structure by matching the parent?

How can I get the output in tree structure by matching the parent? I can only use a single ng-repeat to display this.
My expected output
firstname :-dsfsdf
lastname :- fdsfsdfs
account:
role:-dsfsdf
status:-dsfdfsfds
js
var app = angular.module("test",[]);
app.controller("MainCtrl",function($scope){
$scope.inputs = [
{
"firstname" : "Test"
},{
"lastname" : "Test1"
},{
"Account" : [
{"role" : "Test3"},
{"status" : "Test4"},
]
},
{
"Account1" : [
{"role" : "Test3"},
{"status" : "Test4"},
]
},
{
"Account2" : [
{"role" : {
'dim3': {
'dim4':{
'dim5':'cccc'
}
}
}
},
{"status" : "Test4"},
{"status" : "Test4"},
]
},
{
"ending" : "yes"
}
];
/*$scope.person = [];*/
$scope.myPersonObj = [];
$scope.checkIndex1 = function(arg, myPersonObj)
{
if (angular.isArray(arg) || angular.isObject(arg)) {
angular.forEach(arg, function (value, key) {
console.log(value + "started");
if(angular.isObject(value) || angular.isArray(value))
{
$scope.checkIndex1(value, myPersonObj);
}
else
{
console.log( value + "pushing--test");
myPersonObj.push(arg);
}
});
}
else
{
console.log( value + "pushing1");
myPersonObj.push(arg);
}
}
$scope.checkIndex1($scope.inputs, $scope.myPersonObj);
console.log("myPersonObj :"+ JSON.stringify($scope.myPersonObj));
console.log($scope.inputs[2] + "del");
}):

Aggregation does not working in Mongoose with Match and Group

i'm coding an application using AngularJS, NodeJS and Mongoose.
Now, i'm doing a chart and I need to return some data passsing some parameters.
But the aggregation does not work. I have this query working in MongoDB:
var user = db.getCollection('users').find(
{profile: "SUPERVISOR"},{_id: 1}).map(function(u){return u._id;}
)
db.getCollection("visitas").aggregate(
{ '$match':
{ createdAt:
{ '$gte': ISODate('2015-01-10T00:00:00.000Z'),
'$lt': ISODate('2015-07-01T00:00:00.000Z') } } }, { '$group':
{ _id: { usuario: user, dia: [Object] },
visitas: { '$sum': 1 } } }
)
When I try to run the same query in my application using Mongoose, the query does not work. The problem i've seen is that the comma between MATCH and GROUP is disapearing. When i try to search with only group or match, the query works. Someone can help me? This is the application code:
.then(function (listaUsuarios){
var argsVisit;
console.log({ $match : { "createdAt" : { $gte: "2015-01-10T00:00:00.000Z", $lt: "2015-07-01T00:00:00.000Z" }} },
{ $group : { _id : { "usuario" : listaUsuarios , "dia" : { $dayOfMonth : "$createdAt" } }, "visitas" : { $sum : 1 } } });
Visita.aggregate({ $match : { "createdAt" : { $gte: "2015-01-10T00:00:00.000Z", $lt: "2015-07-01T00:00:00.000Z" }} },
{ $group : { _id : { "usuario" : listaUsuarios , "dia" : { $dayOfMonth : "$createdAt" } }, "visitas" : { $sum : 1 } } })
.exec(function (err, results) {
if (err) {
console.log("Erro!!!");
deferredDados.reject({
error: 500,
message: 'Erro buscando Visita, erro: '+err
});
} else {
if (!results || results.length == 0) {
console.log("Sem retorno!!!");
deferredDados.resolve( [0,0,0,0,0,0,0] ); //Tudo zerado poruqe não há retorno.
//deferredDados.resolve(0);
deferredDados.reject({
error: 500,
message: 'Erro buscando Visitas, não achou resultados'
});
} else {
console.log("Dados"+results);
deferredDados.resolve( results );
//deferredDados.resolve( results.data] );
}
}
});
return deferredDados.promise;
})
SOLVED!!!
A member of my team found the error! \o/
Maybe this can help someone who will have the same problem.
To solve, you just have to convert the date, in string, to the date type.
The solved code is below:
.then(function (listaUsuarios){
var argsVisit;
if (tipoVisita == 'N'){
argsVisit = {'user' : { "$in" :listaUsuarios }, 'tipo' : { "$in" : ["2001","2002"] }, "createdAt": {"$gte": dataInicial, "$lt": dataFinal }};
} else if (tipoVisita == 'R'){
argsVisit = {'user' : { "$in" :listaUsuarios }, 'tipo' : { "$in" : ["2005"] }, "createdAt": {"$gte": dataInicial, "$lt": dataFinal }};
} else if (tipoVisita == 'I'){
argsVisit = {'user' : { "$in" :listaUsuarios }, 'tipo' : { "$in" : ["2003"] }, "createdAt": {"$gte": dataInicial, "$lt": dataFinal }};
}
var initDate = new Date('2015-04-02T00:00:00.000Z');
var endDate = new Date('2015-05-09T00:00:00.000Z');
argsMatch = [{ $match : { "createdAt" : { $gte: initDate, $lt: endDate }} },
{ $group : { _id : { "usuario" : listaUsuarios , "dia" : { $dayOfMonth : "$createdAt" } }, "visitas" : {$sum : { $cond: [ {$or : [ { $eq: [ "$tipo", "2001"] },{ $eq: [ "$tipo","2002"] }] },1,0 ]}}}
}]
Visita.aggregate(argsMatch)
.exec(function (err, results) {
if (err) {
console.log("Erro!!!");
deferredDados.reject({
error: 500,
message: 'Erro buscando Visita, erro: '+err
});
} else {
if (!results || results.length == 0) {
console.log("Sem retorno!!!");
deferredDados.resolve( [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0] ); //Tudo zerado poruqe não há retorno.
//deferredDados.resolve(0);
deferredDados.reject({
error: 500,
message: 'Erro buscando Visitas, não achou resultados'
});
} else {
deferredDados.resolve( results );
}
}
});
return deferredDados.promise;
})
.then(function(dados) {
res.json(dados);
})
.catch(function(fallback) {
console.log('Erro no retorno dos usuarios!!!');
console.log(fallback);
});
I hope this can help someone.

MEAN-Stack MongoDB Sub Array Delete - Works in IDE, not in API

I have a [user] document stored that contains a nested sub-array [profiles],[favorites]. I am simply trying to delete($pull) a favorites from a given profile based on the favorites name.
{
"_id" : ObjectId("558d53eebdd9804820090fa1"),
"name" : "Frank",
"email" : "Frank#FrankTheTank.com",
"profiles" : [
{
"avatar" : "div-male",
"age" : "35",
"gender" : "Male",
"profilename" : "Oly Lifter",
"_id" : ObjectId("558d5404bdd9804820090fa2"),
"favorites" : [
{
"name" : "Power Clean"
},
{
"name" : "Hang Clean"
},
{
"name" : "Clean and Jerk"
}
],
"createdAt" : ISODate("2015-06-26T13:30:44.661Z")
}
],
"createdAt" : ISODate("2015-06-26T13:30:22.884Z"),
"role" : "user",
"__v" : 0
}
Using a MongoDB IDE robomongo, I'm able to successfully remove a favorite item from a known User and Profile ID using this
db.users.update($find: {
'profiles': {
'profiles._id': ObjectId("558d5404bdd9804820090fa2")
},
{
$pull: {
'profiles.$.favorites': {
'name': 'Hang Clean'
}
}
})
However, when I call from my server API using the following syntax, I receive an error, note req.body._id = "558d5404bdd9804820090fa2" and req.body.favorites.name = "Hang Clean"
User.findByIdAndUpdate(_user._id, {
'profiles._id': req.body._id
}, {
$pull: {
'profiles.$.favorites': {
'name': req.body.favorites.name
}
}
}, {
safe: true,
upsert: true
},
function(err, model) {
if (err) {
console.log(err);
return res.status(500).send('Error Deleting Profile');
}
return res.status(200).send('Profile Deleted!');
});
Try updating using the findOneAndUpdate() method since you are supplying the findByIdAndUpdate() method with the wrong parameters: the second argument { 'profiles._id': req.body._id } should be part of the first query object hence you need to use the findOneAndUpdate() method as follows, making sure you convert the string ids into ObjectId's:
var mongoose = require('mongoose');
var id = mongoose.Types.ObjectId(_user._id),
profileId = mongoose.Types.ObjectId(req.body._id),
query = {
"_id": id,
"profiles._id": profileId
},
update = {
"$pull": {
"profiles.$.favorites": { "name": req.body.favorites.name }
}
},
options = { "multi": true, "upsert": true };
User.findOneAndUpdate(query, update, options, function(err, model) {
if(err){
console.log(err);
return res.status(500).send('Error Deleting Profile');
}
return res.status(200).send('Profile Deleted!');
});

Resources