Mongo database call returning incorrect data after reload - reactjs

I am trying to sort some database entries based on a boolean representing if a job is "completed" or not. The mutation functionality is working every time according to the graphQL playground but when the database call is utilized in the application the boolean data console logs as true first but then quickly logs again as false. The playground returns the data as true every time, once it has been changed, but something inside the application is not playing nice. Me and my project partners are freshly out of a coding bootcamp and have been learning successfully at a pretty steady pace but have not been able to figure this one out. Any feedback is appreciated. Thanks!!
const { loading: userLoading, data } = useQuery(QUERY_ME_BASIC);
const { loading: jobsLoading, data: jobsData } = useQuery(GET_JOBS);
var user = {};
var jobs = [];
var completedJobs = [];
var incompleteJobs = [];
if (!userLoading) {
console.log(jobsData)
user = data.me;
}
if(!jobsLoading){
jobs = jobsData.jobs
}
if(jobs){
console.log(jobs)
for (let i = 0; i < jobs.length; i++) {
if (jobs[i].completed === false) {
incompleteJobs.push(jobs[i]);
}
}
}
Query Response (GET_JOBS) from Graphql playground
{
"data": {
"jobs": [
{
"id": "47",
"_id": "6107fce77f7afc40e4e185a7",
"date": "2021-08-17",
"category": "Yard Waste",
"description": "addas",
"distance": "1.585",
"taken": true,
"completed": true,
"phone": "6153050246",
"email": "wfh2d88#gmail.com",
"driverEmail": "wfh2d88#gmail.com",
"createdAt": "Aug 2nd, 2021 at 9:10 am",
"pickup": {
"address": "207 Porter Village Circle",
"address2": "A",
"city": "Nashville",
"state": "Tennessee",
"zip": "37206",
"lat": "36.182571",
"lng": "-86.733267"
},
"dropoff": {
"address": "1800 Stratford Ave",
"address2": "A",
"city": "Nashville",
"state": "Alabama",
"zip": "37206",
"lat": "36.198164",
"lng": "-86.717083"
}
}
]
}
}
console.log(job) after Job Completion (3 appear one quickly after the other). First Two:
0:
category: "Yard Waste"
completed: true
createdAt: "Aug 2nd, 2021 at 9:10 am"
date: "2021-08-17"
description: "addas"
distance: "1.585"
driverEmail: "wfh2d88#gmail.com"
dropoff: {address: "1800 Stratford Ave", address2: "A", city: "Nashville", state: "Alabama", zip: "37206", …}
email: "wfh2d88#gmail.com"
id: "47"
phone: "6153050246"
pickup: {address: "207 Porter Village Circle", address2: "A", city: "Nashville", state: "Tennessee", zip: "37206", …}
taken: true
__typename: "Job"
_id: "6107fce77f7afc40e4e185a7"
[[Prototype]]: Object
length: 1
[[Prototype]]: Array(0)
Profile.js:32
Object
Profile.js:41
Last log from same line:
0:
category: "Yard Waste"
completed: false
createdAt: "Aug 2nd, 2021 at 9:10 am"
date: "2021-08-17"
description: "addas"
distance: "1.585"
driverEmail: "wfh2d88#gmail.com"
dropoff: {address: "1800 Stratford Ave", address2: "A", city: "Nashville", state: "Alabama", zip: "37206", …}
email: "wfh2d88#gmail.com"
id: "47"
phone: "6153050246"
pickup: {address: "207 Porter Village Circle", address2: "A", city: "Nashville", state: "Tennessee", zip: "37206", …}
taken: true
__typename: "Job"
_id: "6107fce77f7afc40e4e185a7"

Related

Using the data array from one array object as part of another array object

I have two models, ExerciseModel and RoutineModel, in which a single routine is comprised of one or more exercises. I am trying to use the contents of the data array from the ExerciseArrayObject class as part of my RoutineArrayObject's data array.
I am receiving the following error:
'self' used in property access 'exercises' before all stored properties are initialized
Here is ExerciseArrayObject:
import Foundation
import SwiftUI
class ExerciseArrayObject: ObservableObject {
#Published var dataArray = [ExerciseModel]()
init() {
//print("FETCH FROM DATABASE HERE")
let exercise1 = ExerciseModel(exerciseID: "", userID: "", username: "userA", exerciseTitle: "Exercise 1", dateCreate: Date(), exerciseImage: "logo", repsInfo: "12 reps", setsInfo: "3 sets")
let exercise2 = ExerciseModel(exerciseID: "", userID: "", username: "userB", exerciseTitle: "Exercise 2", dateCreate: Date(), exerciseImage: "logo", repsInfo: "8 reps", setsInfo: "4 sets")
let exercise3 = ExerciseModel(exerciseID: "", userID: "", username: "userC", exerciseTitle: "Exercise 3", dateCreate: Date(), exerciseImage: "logo", repsInfo: "5 reps", setsInfo: "5 sets")
let exercise4 = ExerciseModel(exerciseID: "", userID: "", username: "userD", exerciseTitle: "Exercise 4", dateCreate: Date(), exerciseImage: "logo", repsInfo: "20 reps", setsInfo: "10 sets")
let exercise5 = ExerciseModel(exerciseID: "", userID: "", username: "userE", exerciseTitle: "Exercise 5", dateCreate: Date(), exerciseImage: "logo", repsInfo: "10 reps", setsInfo: "3 sets", sharedUserUsername: "Shared User")
self.dataArray.append(exercise1)
self.dataArray.append(exercise2)
self.dataArray.append(exercise3)
self.dataArray.append(exercise4)
self.dataArray.append(exercise5)
}
}
And here is RoutineArrayObject:
class RoutineArrayObject: ObservableObject {
#ObservedObject var exercises: ExerciseArrayObject
#Published var dataArray = [RoutineModel]()
init() {
//print("FETCH FROM DATABASE HERE")
let routine1 = RoutineModel(routineID: "", userID: "", username: "user1", routineTitle: "Yoga Routine", exercises: exercises.dataArray, dateCreate: Date(), routineImage: "demoexercise", noOfExercises: "\(exercises.dataArray.count)")
let routine2 = RoutineModel(routineID: "", userID: "", username: "user2", routineTitle: "Core Routine", exercises: exercises.dataArray, dateCreate: Date(), routineImage: "logo", noOfExercises: "\(exercises.dataArray.count)", sharedUserID: "", sharedUserUsername: "Shared User")
self.dataArray.append(routine1)
self.dataArray.append(routine2)
}
}
Assuming ExerciseModel and RoutineModel are structs,
you could try this approach using completion closures. This
keeps your two models and use the contents of the data array from
the ExerciseArrayObject class as part of your RoutineArrayObject's data array.
class ExerciseArrayObject: ObservableObject {
#Published var dataArray = [ExerciseModel]()
init() {
ExerciseArrayObject.fetchExcersices() { arr in
self.dataArray = arr
}
}
static func fetchExcersices(completion: #escaping([ExerciseModel]) -> ()) {
//print("FETCH FROM DATABASE HERE")
var excersices = [ExerciseModel]()
let exercise1 = ExerciseModel(exerciseID: "", userID: "", username: "userA", exerciseTitle: "Exercise 1", dateCreate: Date(), exerciseImage: "logo", repsInfo: "12 reps", setsInfo: "3 sets")
let exercise2 = ExerciseModel(exerciseID: "", userID: "", username: "userB", exerciseTitle: "Exercise 2", dateCreate: Date(), exerciseImage: "logo", repsInfo: "8 reps", setsInfo: "4 sets")
let exercise3 = ExerciseModel(exerciseID: "", userID: "", username: "userC", exerciseTitle: "Exercise 3", dateCreate: Date(), exerciseImage: "logo", repsInfo: "5 reps", setsInfo: "5 sets")
let exercise4 = ExerciseModel(exerciseID: "", userID: "", username: "userD", exerciseTitle: "Exercise 4", dateCreate: Date(), exerciseImage: "logo", repsInfo: "20 reps", setsInfo: "10 sets")
let exercise5 = ExerciseModel(exerciseID: "", userID: "", username: "userE", exerciseTitle: "Exercise 5", dateCreate: Date(), exerciseImage: "logo", repsInfo: "10 reps", setsInfo: "3 sets", sharedUserUsername: "Shared User")
excersices.append(exercise1)
excersices.append(exercise2)
excersices.append(exercise3)
excersices.append(exercise4)
excersices.append(exercise5)
completion(excersices) // <-- todo deal with errors etc...
}
}
class RoutineArrayObject: ObservableObject {
#Published var dataArray = [RoutineModel]()
init() {
ExerciseArrayObject.fetchExcersices() { exercises in
print("--> exerciseArray: \(exercises)")
self.fetchRoutines(exercises: exercises) { _ in
print("--> routineArray: \(self.dataArray)")
}
}
}
func fetchRoutines(exercises: [ExerciseModel], completion: #escaping(Bool) -> ()) {
//print("FETCH FROM DATABASE HERE")
let routine1 = RoutineModel(routineID: "", userID: "", username: "user1", routineTitle: "Yoga Routine", exercises: exercises, dateCreate: Date(), routineImage: "demoexercise", noOfExercises: exercises.count)
let routine2 = RoutineModel(routineID: "", userID: "", username: "user2", routineTitle: "Core Routine", exercises: exercises, dateCreate: Date(), routineImage: "logo", noOfExercises: exercises.count, sharedUserID: "", sharedUserUsername: "Shared User")
self.dataArray.append(routine1)
self.dataArray.append(routine2)
completion(true) // <-- todo deal with errors etc...
}
}
You can also use other techniques instead of basic completion closures, such as
async/await concurrency with tasks.

How to place woocommerce order with meta_data with axios

i am creating a resturant delivery react native app using woocommerce api and trying to place an order with Woocommerce API using axios.
on the website, i can select a product, its optional dishes and place the order. and it creates successfully..
but in the app i do like this.
{
payment_method: "cod",
payment_method_title: "Cash On Delivery",
set_paid: true,
billing: {
first_name: "John",
last_name: "Doe",
address_1: "969 Market",
address_2: "",
city: "San Francisco",
state: "CA",
postcode: "94103",
country: "US",
email: "john.doe#example.com",
phone: "(555) 555-5555"
},
shipping: {
first_name: "John",
last_name: "Doe",
address_1: "969 Market",
address_2: "",
city: "San Francisco",
state: "CA",
postcode: "94103",
country: "US"
},
line_items: [
{
product_id: 1479,
quantity: 2
},
],
shipping_lines: [
{
method_id: "flat_rate",
method_title: "Flat Rate",
total: "10.00"
}
]
};
it creates the order successfully but only shows the side items without calculating the price. means only the basic price is shown on the order without adding the additional products.

Order of data returning from Monongodb

I am making a mongodb model>>
const mongoose = require('mongoose');
const {Schema} = mongoose;
const locationSchema = new Schema({
name: String,
Address: String,
ContactInfo: {
phone: Number,
email: String,
},
Website: String,
Hours: {
DaysOpen: String,
OpeningTime:[Number],
ClosingTime:[Number],
},
Services: String,
Languages: String,
Documentation: Boolean,
OtherNotes: String,
})
mongoose.model('Locations', locationSchema);
When I try and run a get request to see what is in my database I am returned
{
"error": false,
"location": {
"Hours": {
"OpeningTime": [
1215,
898
],
"ClosingTime": [
1400
],
"DaysOpen": "Sunday"
},
"_id": "5ee8fd2e57aa5126d4c1c854",
"name": "Evergreen Christian Center Food Pantry",
"Address": "4400 NW Glencoe Rd, Hillsboro, OR 97124",
"ContactInfo": {
"phone": 5033196590,
"email": "gonzocyn2#msn.com"
},
"Website": "https://www.ecc4.org/home",
"Services": "All foods available including meat and frozen foods",
"Languages": "English, Spanish",
"Documentation": false,
"OtherNotes": "Bring own bag or box. Sign up starts at 9:00am",
"__v": 0
}
The problem is that "Hours" is being displayed before the name, address, and contact info. This only occurs when I have the fields "OpeningTime" and "ClosingTime" as arrays. Any idea on how to fix this?

Create multidimensional array based on value of key

I have an array of MyData objects (MyData is a struct):
[
MyData(id: 3, locale: "en", title: "p1", date: "10/15/2019"),
MyData(id: 3, locale: "de", title: "p2", date: "11/12/2019"),
MyData(id: 32, locale: "fr", title: "free", date: "10/11/2019"),
MyData(id: 15, locale: "de", title: "free", date: "10/11/2019"),
MyData(id: 19, locale: "de", title: "p1", date: "11/10/2019"),
MyData(id: 19, locale: "de", title: "p2", date: "11/10/2019"),
MyData(id: 19, locale: "de", title: "p3", date: "11/10/2019"),
]
I'd like to group this array (or even purposedly create a new one) based on the id key.
The result should be like this one:
[
[MyData(id: 3, locale: "en", title: "p1", date: "10/15/2019"), MyData(id: 3, locale: "de", title: "p2", date: "11/12/2019")],
MyData(id: 32, locale: "fr", title: "free", date: "10/11/2019"),
MyData(id: 15, locale: "de", title: "free", date: "10/11/2019"),
[MyData(id: 19, locale: "de", title: "p1", date: "11/10/2019"),MyData(id: 19, locale: "de", title: "p2", date: "11/10/2019"),MyData(id: 19, locale: "de", title: "p3", date: "11/10/2019")]
]
that is: arrays having the same id should form a new array.
Of course, I could simply loop over the first array and create the second, but I wanted to know if there's something Swift can do with its filters.
Any help is appreciated.
You can use the higher order functions in this for sure, but not 100% to fully produce the desired result but a big chunk of it, since your desired array type is :[Any].
Check out the code below:
var myGroup = Dictionary(grouping: arrayOne, by: { $0.id }) // group each element by id -type of: [Int:[Data]]
let resultArray = myGroup.map { $0.value } //map out the elements without the id key. -type of: [[Data]]
//Create hetro Array so we can use it later to append the results
var myHetroArray: [Any] = []
// loop each array in the result array and check if it only contains 1 element if so append that one element to the hetro array otherwise just append the whole thing.
for array in resultArray {
if array.count == 1 {
myHetroArray.append(array.first!)
} else {
myHetroArray.append(array)
}
}
print(myHetroArray) // produce the desired result.
Output: [
[Data(id: 19, locale: "de", title: "p1", date: "11/10/2019"),Data(id: 19, locale: "de", title: "p2", date: "11/10/2019"),Data(id: 19, locale: "de", title: "p3", date: "11/10/2019")],
Data(id: 15, locale: "de", title: "free", date: "10/11/2019"),
Data(id: 32, locale: "fr", title: "free", date: "10/11/2019"),
[Data(id: 3, locale: "en", title: "p1", date: "10/15/2019"), Data(id: 3, locale: "de", title: "p2", date: "11/12/2019")]
]
There are two more ways. Just as a note here.
struct MyData{
let id : Int
let locale : String
let title : String
let date : String
}
let data = [
MyData(id: 3, locale: "en", title: "p1", date: "10/15/2019"),
MyData(id: 3, locale: "de", title: "p2", date: "11/12/2019"),
MyData(id: 32, locale: "fr", title: "free", date: "10/11/2019"),
MyData(id: 15, locale: "de", title: "free", date: "10/11/2019"),
MyData(id: 19, locale: "de", title: "p1", date: "11/10/2019"),
MyData(id: 19, locale: "de", title: "p2", date: "11/10/2019"),
MyData(id: 19, locale: "de", title: "p3", date: "11/10/2019"),
]
data.reduce(into: [:]) { ( result, next) in
if result.keys.contains(next.id){
(result[next.id] as? [MyData]).map{result[next.id] = $0 + [next] }
([result[next.id]] as? [MyData]).map{result[next.id] = $0 + [next]}
}
else{ result[next.id] = next }
}.values
Set(data.map{$0.id}).map{id -> Any in
let result = data.filter{$0.id == id}
return result.count == 1 ? result.first! : result
}

Reference Error : Object is not Defined While Accessing a Object with "|" in between Angular (Typescript)

Friends, I am Trying to Access an object out of a nested array, everything seems to be fine but I can't access one variable which includes a |
Below i am Showing the JSON Data.
var data = {
isbaggage: "true",
O: {
"BOM|DEL": {
uid: "1",
rph: "1",
baggageOptions: [
{
typ: "XC30",
desc: "30 kg",
amt: "11400",
curr: "INR",
convamt: "11400"
},
{
typ: "XC05",
desc: "5 kg",
amt: "1900",
curr: "INR",
convamt: "1900"
},
{
typ: "XC10",
desc: "10 kg",
amt: "3800",
curr: "INR",
convamt: "3800"
},
{
typ: "XC15",
desc: "15 kg",
amt: "5700",
curr: "INR",
convamt: "5700"
}
]
}
}
};
This is the code I am trying to access it with.
console.log(data.O.BOM|DEL);
This is the link of Sandbox link
Sandbox Link
You have to write this way,
console.log(data.O['BOM|DEL']);

Resources