Sequalize save array of items - database

I have many to many relations between the two tables actors and movies I try to execute this code but sequalize don't save actors
const arr = [];
actors.forEach((element:string) => {
const x = this.actorsService.createActor(element);
arr.push(x);
});
await Promise.all(arr);
const movie = await this.moviesRepository.create({
...dto,
actors: arr,
});
Firstly, I get an array of strings (actors); for each element, I create a new actor inside createActor. Later I want to put that data into a movie but anything I don't save

The arr still stores promises and not the actor instances. You need to store the result of Promise.all to get actor instances:
const arr = await Promise.all(actors.map(x => this.actorsService.createActor(x)));
const movie = await this.moviesRepository.create({
...dto,
actors: arr,
});

Related

Get array of ids from object array in firestore

in firestore I have a document with two strings and also an object array with [{email: ..., id: ..., nickname: ...} {...}]
I use a subscription to get all users from that specific document. The next step is to extract all ids from that object array (called "users") in a new array.. But I have no idea how to do this.
I try somethink like this:
this.group.forEach(element => console.log(element)
"this.group" is the subscription of that document. But this output display all content from this document and not the only array called (users)See attachement.
Hope anyone can help?
You can loop it and push it in a new array like this. See sample code below:
const docSnap = await getDoc(docRef);
const newArray = [];
if (docSnap.exists()) {
const users = docSnap.data().users
users.forEach(element => {
newArray.push(element.id);
})
// This will return an array of `id` from the `users` array.
console.log(newArray);
} else {
// doc.data() will be undefined in this case
console.log("No such document!");
}
The same logic will be applied if you use it to the subscription of the document.

I need to compare two arrays/state and get the data from the first array/state that doesn't match with the data on the second array - Reactjs

So I'm working with states in React, and i want to compare the two arrays/state and get the data from the first array that doesn't match with the data on the second array.
The first array is a state, and the second one is an array.
My first state/array looks like this = state : [ { id : id , productNumber : "productnumber"}... ] and the second array loos like this = array ["productnumber","productnumber"...],
and here what is my code's look like.
let newArray = [];
state.forEach(product=>{
array.forEach(productNumber=>{
if(product.productNumber !== productNumber){
newArray = [...newArray,product.productNumber];
}
})
})
and it doesn't work, it stores all the data from the first array/state and even doubled it. I tried using nested if statements, still doesn't. I don't know what todo.
let newArray = [];
state.forEach(({productNumber}) => {
if (array.indexOf(productNumber) === -1) {
newArray = [...newArray, productNumber];
}
})
Or similarly, you can filter the state array and return the products with productNumber not in your second array.
const newArray = state.filter(({productNumber}) => array.indexOf(productNumber) === -1);

For...in loop is adding keys instead of values when sending to Firestore

I have an object, which contains some variables and an array. I'm trying to add the data to Firebase. Firebase didn't like the array. My solution was to convert the array to an object and send the object values to an array in Firestore using a For...in loop and a Firestore array query. This almost worked but, for some reason, my code is adding the key names instead of the values, What am I doing wrong?
let projectId = Math.floor(Math.random() * 10000);
let docTitle = this.projectTitle.title;
this.pages.forEach((page) => {
let pageT = page.pageTitle;
let pageD = page.pageDesc;
let id = page.id;
let blocks = page.blocks;
db.collection(docTitle + projectId).doc(pageT).set({
page: {pageTitle:pageT, pageDesc: pageD, id:id }
})
.then(function() {
console.log("Document successfully written!");
})
.catch(function(error) {
console.error("Error writing document: ", error);
});
// Converts array into object and adds object values to an array in firestore
let newBlocks = Object.assign({}, blocks);
for(let block in newBlocks){
db.collection(docTitle + projectId).doc(pageT).update({
blocks: firebase.firestore.FieldValue.arrayUnion(block)
})
}
})
Here are the Firestore docs on updating arrays: https://firebase.google.com/docs/firestore/manage-data/add-data#update_elements_in_an_array
Use for…of to iterate over the values in an iterable,
Change
for(let block in newBlocks){
To
for(let block of newBlocks){
for...in iterates over object keys hence for...of correct in your case

Rxjs Scan method copies content of array property of Observable when new items added

In an Angular component I have a property of type BehaviourSubject<IController>:
controllerData$ = new BehaviourSubject<IController | null>(null);
IController {
users: IUser[];
showDetails: boolean;
}
I need to iterate over the users array of type IUser and execute an intermediate transformation over the property data before returning the results as array.
Below the code I use:
flatMap(): to flatten the IUsers[] items
map(): to iterate over the users and apply the intermediate processing
scan(): to pack the results into an array again
Below my code:
controllerData$
.pipe(
filter((input) => input !== null),
flatMap((p) => {
return p.users;
}),
map((m: IUser) => {
const data = {m,
date: this.getWeekDay(m.occurrence),
nextOccurrence: this.service.getNextDate(m.frequency, m.occurrence),
};
return data;
}),
scan((acc: any, cur) => [...acc, cur], [])
)
When the component loads the first time with two IUser items, everything works well. However when I add a new user to the array, the scan method copies also the previous array values, that is, having two initial items and adding a new one, I get 5 items in the array (the first two twice and the new one) instead of just three elements.
As a workaround I achieved it with the solution below, but I was wondering whether I can skip the foreach loop and obtain everything with just Rxjs operators:
.pipe(
filter((input) => input !== null),
map((m) => {
const results: any[] = [];
m.users.forEach((ctlr: IUser) => {
results.push({
user: ctlr.user,
date: this.getWeekDay(ctlr.occurrence),
nextOccurrence: this.service.getNextDateOccurrence(ctlr.frequency, ctlr.occurrence),
});
});
return results;
}),

React multidimensonal array filter/find

I have a multidimensional array (list of matchs with players) and i need to filter this array to only get unique player object.
The following code works but the result i expected is different.
Const aPlayer=matchs
.filter(match => match.players.find(player => player.id
===id))
The variable aPlayer contains all matchs for this player.
But i only need player object data.
I'm not sure if your data structured like this:
Matches: List<Match>
Match: {
...,
players: List<Player>
}
Player: {
id: Index,
...
}
If so, you can do it like this:
// still contains duplicates, but that's not the issue here
const allPlayers = matchs
.map(x => x.players)
.reduce(x => (dest, val) => dest.concat(val), []);
const player = allPlayers.find(x => x.id === id);

Resources