Find In array TypeOrm - nestjs

Let's see if you can help me, because I've been like this for a few hours and I can't.
I'm doing some queries with Typeorm, and this is the situation:
I have an array of Ids: ["array1, array1, array3"]. that is, in the array an id is repeated 2 times.
The thing is that if I make a query such that:
const found = await this.find ({
where: {id: In (Ids)},
})
In found what I have is:
[
Catalog {
id: 'array1',
image: 'https://image.jpg',
name: 'Article name',
description: 'Description',
price: 809,
createdAt: 2020-12-04T17:34:16.869Z,
updatedAt: 2020-12-04T17:34:16.869Z },
Catalog {
id: 'array3',
image: 'https://image.jpg',
name: 'Article name',
description: 'Description',
price: 809,
createdAt: 2020-12-04T17:34:16.869Z,
updatedAt: 2020-12-04T17:34:16.869Z }
]
And that I need is:
[
Catalog {
id: 'array1',
image: 'https://image.jpg',
name: 'Article name',
description: 'Description',
price: 809,
createdAt: 2020-12-04T17:34:16.869Z,
updatedAt: 2020-12-04T17:34:16.869Z },
Catalog {
id: 'array1',
image: 'https://image.jpg',
name: 'Article name',
description: 'Description',
price: 809,
createdAt: 2020-12-04T17:34:16.869Z,
updatedAt: 2020-12-04T17:34:16.869Z },
Catalog {
id: 'array3',
image: 'https://image.jpg',
name: 'Article name',
description: 'Description',
price: 809,
createdAt: 2020-12-04T17:34:16.869Z,
updatedAt: 2020-12-04T17:34:16.869Z }
]
Another option would be to do next a method that receiving the parameters of the ID array and the object, iterate to obtain the array of objects that I need ... but I have not found the key either.
Thanks for you help

Finally, i do this:
const order: CatalogDto[] = []
for (let i = 0; i < Ids.length; i++) {
const found: CatalogDto = uniqueProduct.find(product=> product.id == Ids[i])
order.push(found)
}
where uniqueProduct is :
[
Catalog {
id: 'array1',
image: 'https://image.jpg',
name: 'Article name',
description: 'Description',
price: 809,
createdAt: 2020-12-04T17:34:16.869Z,
updatedAt: 2020-12-04T17:34:16.869Z },
Catalog {
id: 'array3',
image: 'https://image.jpg',
name: 'Article name',
description: 'Description',
price: 809,
createdAt: 2020-12-04T17:34:16.869Z,
updatedAt: 2020-12-04T17:34:16.869Z }
]
and in order I have:
[
Catalog {
id: 'array1',
image: 'https://image.jpg',
name: 'Article name',
description: 'Description',
price: 809,
createdAt: 2020-12-04T17:34:16.869Z,
updatedAt: 2020-12-04T17:34:16.869Z },
Catalog {
id: 'array1',
image: 'https://image.jpg',
name: 'Article name',
description: 'Description',
price: 809,
createdAt: 2020-12-04T17:34:16.869Z,
updatedAt: 2020-12-04T17:34:16.869Z },
Catalog {
id: 'array3',
image: 'https://image.jpg',
name: 'Article name',
description: 'Description',
price: 809,
createdAt: 2020-12-04T17:34:16.869Z,
updatedAt: 2020-12-04T17:34:16.869Z }
]

Related

Object.values map data node js

I have to use the data I receive, in particular I have to be able to use the data in 'properties':
I don't understand how to call them, instead of console.log
I tried to use an Object.values(), but so I can't access the 'properties' data that I have to reuse later.
const response = await notion.databases.query({
database_id: databaseId,
page_size: 2
});
var effort_data = []
effort_data.push({
response
})
effort_data.map(data => {
Object.values(data.response.results).map((data) => {
console.log('data', data)
})
})
this is my response :
data {
object: 'page',
id: '3b8e203e-79ae-4ddc-ba3a-e0f5b30f4d4e',
created_time: '2023-01-25T13:51:00.000Z',
last_edited_time: '2023-01-25T13:59:00.000Z',
created_by: { object: 'user', id: '204c1b2e-0c59-4605-be45-de1fc9916416' },
last_edited_by: { object: 'user', id: '204c1b2e-0c59-4605-be45-de1fc9916416' },
cover: null,
icon: { type: 'emoji', emoji: '👽' },
parent: {
type: 'database_id',
database_id: '1b02ff59-8cdb-47fb-a6f9-5904bcecbe72'
},
archived: false,
properties: {
Assignees: { id: '%24v1Q', type: 'people', people: [] },
Type: { id: '9dB%5E', type: 'select', select: [Object] },
Progetti: {
id: '%3AirU',
type: 'relation',
relation: [Array],
has_more: false
},
Epics: { id: 'B%7BSE', type: 'relation', relation: [], has_more: false },
'Creato da': { id: 'D%5Dzr', type: 'created_by', created_by: [Object] },
'Flussi coinvolti': { id: 'EFdR', type: 'relation', relation: [], has_more: false },
'Sprint corrente?': { id: 'G%5Cvg', type: 'formula', formula: [Object] },
Sprint: { id: 'Jz.%40', type: 'multi_select', multi_select: [] },
'🏃 Sprint': { id: 'O%3DHc', type: 'relation', relation: [], has_more: false },
Settore: { id: 'XYh%5D', type: 'select', select: null },
Timeline: { id: '_G%2Bl', type: 'date', date: [Object] },
Tasks: {
id: 'bXv%3F',
type: 'relation',
relation: [Array],
has_more: false
},
'Data creazione': {
id: 'e_r%7D',
type: 'created_time',
created_time: '2023-01-25T13:51:00.000Z'
},
'Epic status': { id: 'nRGT', type: 'rollup', rollup: [Object] },
'Ultimo aggiornamento da': { id: 'ogbh', type: 'last_edited_by', last_edited_by: [Object] },
'Data ultimo aggiornamento': {
id: 'p%5CYK',
type: 'last_edited_time',
last_edited_time: '2023-01-25T13:59:00.000Z'
},
Priority: { id: '%7BMEq', type: 'select', select: [Object] },
Status: { id: '%7CF4-', type: 'select', select: [Object] },
Name: { id: 'title', type: 'title', title: [Array] }
},
url: 'https://www.notion.so/Endpoint-per-ordini-card-da-esterni-3b8e203e79ae4ddcba3ae0f5b30f4d4e'
}

Node JS remove a specific object from array (mongoose)

I have an array of objects - lists[]; and an object - obj;
This is the lists array:
{
_id: new ObjectId("61840c5ce237f22a1c7a1ac7"),
name: 'list1',
content: [ 'aaaa' ],
description: 'aa',
tags: [],
lastmodified: 1,
__v: 0
},{
_id: new ObjectId("61840def80a88d1b2ffce400"),
name: 'list',
content: [ 'list!' ],
description: 'test',
tags: [],
__v: 0
}
and this is the obj object:
{
_id: new ObjectId("61840def80a88d1b2ffce400"),
name: 'list',
content: [ 'list!' ],
description: 'test',
tags: [],
__v: 0
}
Simple Question: How do I delete the object from "lists", similar to the "obj" one
You could use the JavaScript Array.filter() method to exclude the target object from the list:
const list = [{
_id: new ObjectId("61840c5ce237f22a1c7a1ac7"),
name: 'list1',
content: [ 'aaaa' ],
description: 'aa',
tags: [],
lastmodified: 1,
__v: 0
},{
_id: new ObjectId("61840def80a88d1b2ffce400"),
name: 'list',
content: [ 'list!' ],
description: 'test',
tags: [],
__v: 0
}];
const targetObj = {
_id: new ObjectId("61840def80a88d1b2ffce400"),
name: 'list',
content: [ 'list!' ],
description: 'test',
tags: [],
__v: 0
};
const filteredList = list.filter((element) => element._id !== targetObj._id);
use some sort of filtering... heres an idea.
items = [{
_id: new ObjectId("61840c5ce237f22a1c7a1ac7"),
name: 'list1',
content: [ 'aaaa' ],
description: 'aa',
tags: [],
lastmodified: 1,
__v: 0
},{
_id: new ObjectId("61840def80a88d1b2ffce400"),
name: 'list',
content: [ 'list!' ],
description: 'test',
tags: [],
__v: 0
}]
removeID = "61840def80a88d1b2ffce400"
items = [item for item in items if item['id'] != removeID]

firebase callable node function

I try to use a node function in firebase callable function and wehn I try to use it as described I see this warning on VS code. any help? on what I can do
module "c:/Users/firat/Desktop/vuedg3/functions/node_modules/iyzipay/lib/Iyzipay"
Could not find a declaration file for module 'iyzipay'. 'c:/Users/firat/Desktop/vuedg3/functions/node_modules/iyzipay/lib/Iyzipay.js' implicitly has an 'any' type.
Try npm install #types/iyzipay if it exists or add a new declaration (.d.ts) file containing declare module 'iyzipay';ts(7016)
and this callable function return null when I try and call it from web app
this is my entire code
exports.trypay = functions.https.onCall((data, context) => {
var Iyzipay = require('iyzipay');
var iyzipay = new Iyzipay({
apiKey: "sandbox-11",
secretKey: "sandbox-11",
uri: 'https://sandbox-api.iyzipay.com'
});
var request = {
locale: Iyzipay.LOCALE.TR,
conversationId: '123456789',
price: '1',
paidPrice: '1.2',
currency: Iyzipay.CURRENCY.TRY,
installment: '1',
basketId: 'B67832',
paymentChannel: Iyzipay.PAYMENT_CHANNEL.WEB,
paymentGroup: Iyzipay.PAYMENT_GROUP.LISTING,
paymentCard: {
cardHolderName: 'John Doe',
cardNumber: '5528790000000008',
expireMonth: '12',
expireYear: '2030',
cvc: '123',
registerCard: '0'
},
buyer: {
id: 'BY789',
name: 'John',
surname: 'Doe',
gsmNumber: '+905350000000',
email: 'email#email.com',
identityNumber: '74300864791',
lastLoginDate: '2015-10-05 12:43:35',
registrationDate: '2013-04-21 15:12:09',
registrationAddress: 'Nidakule Göztepe, Merdivenköy Mah. Bora Sok. No:1',
ip: '85.34.78.112',
city: 'Istanbul',
country: 'Turkey',
zipCode: '34732'
},
shippingAddress: {
contactName: 'Jane Doe',
city: 'Istanbul',
country: 'Turkey',
address: 'Nidakule Göztepe, Merdivenköy Mah. Bora Sok. No:1',
zipCode: '34742'
},
billingAddress: {
contactName: 'Jane Doe',
city: 'Istanbul',
country: 'Turkey',
address: 'Nidakule Göztepe, Merdivenköy Mah. Bora Sok. No:1',
zipCode: '34742'
},
basketItems: [
{
id: 'BI101',
name: 'Binocular',
category1: 'Collectibles',
category2: 'Accessories',
itemType: Iyzipay.BASKET_ITEM_TYPE.PHYSICAL,
price: '0.3'
},
{
id: 'BI102',
name: 'Game code',
category1: 'Game',
category2: 'Online Game Items',
itemType: Iyzipay.BASKET_ITEM_TYPE.VIRTUAL,
price: '0.5'
},
{
id: 'BI103',
name: 'Usb',
category1: 'Electronics',
category2: 'Usb / Cable',
itemType: Iyzipay.BASKET_ITEM_TYPE.PHYSICAL,
price: '0.2'
}
]
};
iyzipay.payment.create(request, function (err, result) {
return('result:'+result + 'error:'+err);
});
})

How to filter nested objects in array of a document in mongoose?

My data stored for user is in the following format :
{
name: 'XYZ',
email: 'abc#gmail.com',
password: 'encrypted',
categories: [
{
name: 'category1',
type: 'type1',
},
{
name: 'category2',
type: 'type2',
}
],
transactions: [
{
date: 2020-04-09T06:00:30.000Z,
type: 'type1',
category: 'category1',
description: 'some desc',
},
{
date: 2020-04-08T06:00:30.000Z,
type: 'type2',
category: 'category1',
description: 'some desc',
},
{
date: 2020-04-07T06:00:30.000Z,
type: 'type3',
category: 'category1',
description: 'some desc',
}
]
}
I want to get the data of particular user after providing unique email but along with that I want transactions filtered between 2 dates.
Example I want data of user with email = abc#gmail.com and transactions of dates between 2020-04-08 and 2020-04-09.
Sample output looks like this :
{
name: 'XYZ',
email: 'abc#gmail.com',
categories: [...],
transactions: [
{
date: 2020-04-09T06:00:30.000Z,
type: 'type1',
category: 'category1',
description: 'some desc',
},
{
date: 2020-04-08T06:00:30.000Z,
type: 'type2',
category: 'category1',
description: 'some desc',
}]
}
Or only transactions between the given dates for the given user email is also fine.
Tried using this query
User.aggregate([
{$match: {email: email}},
{$unwind: '$transactions'},
{$match: {'transactions.date': {$gt: startDate, $lt: endDate}}},
{
$group: {
_id: '$_id',
transactions: {
$type: "$transactions.type",
$category: "$transactions.category",
$date: "$transactions.date",
$description: "$transactions.decription"
}
}
}
]).exec((err, data) => {
if (err) throw err;
console.log(data);
})
Getting the error as follows The field 'transactions' must specify one accumulator

Sails.js Same Model many to many association

Sails.js .10 rc8
I've completely run out of ideas for this
I have a model called User and I want to associate it in a collection to other users like a friends list. Like a many-to-many association
//User.js
friends: {
collection: 'user',
via: 'friends'
}
but when I run
.populate('friends')
it doesn't populate anything. Any ideas?
I find that the best way to implement this is actually adding a reference to the id.
Check this User model:
module.exports = {
attributes: {
name: {
type: 'string',
required: true,
minLength: 2
},
email: {
type: 'email',
required: true,
unique: true
},
friends: {
collection: 'user',
via: 'id'
}
}
};
Now, if you run sails console you can test the following commands:
User.create({name:'test',email:'test#test.com'}).exec(console.log);
It returns:
{ name: 'test',
email: 'test#test.com',
createdAt: '2016-12-01T22:06:19.723Z',
updatedAt: '2016-12-01T22:06:19.723Z',
id: 1 }
You created your first user. Lets create some other ones:
User.create({name:'test2',email:'test2#test.com'}).exec(console.log);
Resulting in:
{ name: 'test2',
email: 'test2#test.com',
createdAt: '2016-12-01T22:06:40.808Z',
updatedAt: '2016-12-01T22:06:40.808Z',
id: 2 }
Let's see what we have so far:
User.find().populate('friends').exec(console.log);
[ { friends: [],
name: 'test',
email: 'test#test.com',
createdAt: '2016-12-01T22:06:19.723Z',
updatedAt: '2016-12-01T22:06:19.723Z',
id: 1 },
{ friends: [],
name: 'test2',
email: 'test2#test.com',
createdAt: '2016-12-01T22:06:40.808Z',
updatedAt: '2016-12-01T22:06:40.808Z',
id: 2 } ]
Now, let's create a user with a friend, using the reference to the first user. Notice that I just pass a single id, not an array:
User.create({name:'test3',email:'test3#test.com', friends:1}).exec(console.log);
Now, the result is this one. Notice that "friends" does not appear. This is by design.
{ name: 'test3',
email: 'test3#test.com',
createdAt: '2016-12-01T22:07:34.988Z',
updatedAt: '2016-12-01T22:07:34.994Z',
id: 3 }
Let's do a find with populate to see the current status:
User.find().populate('friends').exec(console.log);
Now we see that the third user has friends. The others have an empty array.
[ { friends: [],
name: 'test',
email: 'test#test.com',
createdAt: '2016-12-01T22:06:19.723Z',
updatedAt: '2016-12-01T22:06:19.723Z',
id: 1 },
{ friends: [],
name: 'test2',
email: 'test2#test.com',
createdAt: '2016-12-01T22:06:40.808Z',
updatedAt: '2016-12-01T22:06:40.808Z',
id: 2 },
{ friends:
[ { name: 'test',
email: 'test#test.com',
createdAt: '2016-12-01T22:06:19.723Z',
updatedAt: '2016-12-01T22:06:19.723Z',
id: 1 } ],
name: 'test3',
email: 'test3#test.com',
createdAt: '2016-12-01T22:07:34.988Z',
updatedAt: '2016-12-01T22:07:34.994Z',
id: 3 } ]
Let's create a fourth one, this time with two friends:
User.create({name:'test4',email:'test4#test.com', friends:[1,2]}).exec(console.log);
Resulting in (again, no friends property):
{ name: 'test4',
email: 'test4#test.com',
createdAt: '2016-12-01T22:07:50.539Z',
updatedAt: '2016-12-01T22:07:50.542Z',
id: 4 }
This time, we passed an array of ids to friends. Let's see the current status:
User.find().populate('friends').exec(console.log);
[ { friends: [],
name: 'test',
email: 'test#test.com',
createdAt: '2016-12-01T22:06:19.723Z',
updatedAt: '2016-12-01T22:06:19.723Z',
id: 1 },
{ friends: [],
name: 'test2',
email: 'test2#test.com',
createdAt: '2016-12-01T22:06:40.808Z',
updatedAt: '2016-12-01T22:06:40.808Z',
id: 2 },
{ friends:
[ { name: 'test',
email: 'test#test.com',
createdAt: '2016-12-01T22:06:19.723Z',
updatedAt: '2016-12-01T22:06:19.723Z',
id: 1 } ],
name: 'test3',
email: 'test3#test.com',
createdAt: '2016-12-01T22:07:34.988Z',
updatedAt: '2016-12-01T22:07:34.994Z',
id: 3 },
{ friends:
[ { name: 'test',
email: 'test#test.com',
createdAt: '2016-12-01T22:06:19.723Z',
updatedAt: '2016-12-01T22:06:19.723Z',
id: 1 },
{ name: 'test2',
email: 'test2#test.com',
createdAt: '2016-12-01T22:06:40.808Z',
updatedAt: '2016-12-01T22:06:40.808Z',
id: 2 } ],
name: 'test4',
email: 'test4#test.com',
createdAt: '2016-12-01T22:07:50.539Z',
updatedAt: '2016-12-01T22:07:50.542Z',
id: 4 } ]
Now, how do you add a friend to an existing user? It's somehow odd: you have to first find the user, then use the add function on the resulting model, and then, save it. In my day to day code, I have a helper function that does just that. So, here is the example:
User.findOne(1).populate('friends').exec(function(err,u){ u.friends.add(3);u.save(function(err){ if(err) console.error(err);});});
Now in the console we see no results.
Let's check the User content now:
User.find().populate('friends').exec(console.log);
[ { friends:
[ { name: 'test3',
email: 'test3#test.com',
createdAt: '2016-12-01T22:07:34.988Z',
updatedAt: '2016-12-01T22:07:34.994Z',
id: 3 } ],
name: 'test',
email: 'test#test.com',
createdAt: '2016-12-01T22:06:19.723Z',
updatedAt: '2016-12-01T22:09:41.410Z',
id: 1 },
{ friends: [],
name: 'test2',
email: 'test2#test.com',
createdAt: '2016-12-01T22:06:40.808Z',
updatedAt: '2016-12-01T22:06:40.808Z',
id: 2 },
{ friends:
[ { name: 'test',
email: 'test#test.com',
createdAt: '2016-12-01T22:06:19.723Z',
updatedAt: '2016-12-01T22:09:41.410Z',
id: 1 } ],
name: 'test3',
email: 'test3#test.com',
createdAt: '2016-12-01T22:07:34.988Z',
updatedAt: '2016-12-01T22:07:34.994Z',
id: 3 },
{ friends:
[ { name: 'test',
email: 'test#test.com',
createdAt: '2016-12-01T22:06:19.723Z',
updatedAt: '2016-12-01T22:09:41.410Z',
id: 1 },
{ name: 'test2',
email: 'test2#test.com',
createdAt: '2016-12-01T22:06:40.808Z',
updatedAt: '2016-12-01T22:06:40.808Z',
id: 2 } ],
name: 'test4',
email: 'test4#test.com',
createdAt: '2016-12-01T22:07:50.539Z',
updatedAt: '2016-12-01T22:07:50.542Z',
id: 4 } ]
With this method, you can even add the same user to the friends collection!
User.findOne(1).populate('friends').exec(function(err,u){ u.friends.add(1);u.save(function(err){ if(err) console.error(err);});});
Have fun!
Your models should look like this...
//User.js
friends: {
collection: 'friend',
via: 'user'
}
//Friend.js
user: {
model: 'user'
via: 'friends'
}
Also sails 10 rc8 is old. You should be on sails 10.5
You should use .populate('friends') instead of .populate(friends)
As it turns out, when you have a many to many association in waterline you have to declare one of the models as dominant. Since they are the same model, neither is dominated. While a join table is created, there is no way to populate it.

Resources