I cant add object to realm with primary key - reactjs

I have this kind of object:
export default class Route {
public id: number = 0;
public name: string = "";
public points: Point[] = []
public static schema: Realm.ObjectSchema = {
name: 'Route',
primaryKey: 'id',
properties: {
id: 'int',
name: 'string',
points: { type: 'list', objectType: 'Point' },
}
}
}
So, when i tried to add this object to realm db i've got such error: Cannot modify primary key after creation: 'Route.id'
realm.write(() => {
let id = realm.objects(Route.schema.name).length + 1;
let route = realm.create<Route>(Route.schema.name, {
id: id,
name: `Route ${id}`,
points: [],
});
});
So, what i missed?

Related

Cannot insert bigint in SQL Server using typeorm (NestJS)

I want to insert record in SQL Server using typeorm where column has type bigint. I have 'Cats' entity with id type: bigint.
import { Column, Entity } from 'typeorm';
#Entity('Cats')
export class CatsEntity {
#Column({ type: 'bigint', name: 'CatID' })
public id: string;
#Column('int', { primary: true, name: 'CatDB' })
public db: number;
#Column('varchar', { name: 'Name' })
public name: string;
#Column('datetime', { name: 'DDB_LAST_MOD' })
public ddbLastMod: Date;
}
And dto, that I get in controller:
export class InsertCatsDto {
public id: string;
public db: number;
public name: string;
}
Saving is performed in controller:
#Post('/cats')
public async insertEobResponse(#Body() insertCatsDto: InsertCatsDto): Promise<any> {
const cats = new CatsEntity();
cats.id = insertCatsDto.id;
cats.db = insertCatsDto.db;
cats.name = insertCatsDto.name;
cats.ddbLastMod = new Date();
return this.catsRepository.insert(cats);
}
But when I send request with id as a string via Postman I get the following error:
"error": "Error: Validation failed for parameter '0'. Value must be between -9007199254740991 and 9007199254740991, inclusive. For smaller or bigger numbers, use VarChar type."
I'm not sure if I'm just missing something or need to make some transformation of values or if this is a real issue with typeorm.
To make your code work with bigInt in typeorm you just need to change type in entity from "bigint" to "varchar":
import { Column, Entity } from 'typeorm';
#Entity('Cats')
export class CatsEntity {
#Column({ type: 'varchar', name: 'CatID' })
public id: string;
#Column('int', { primary: true, name: 'CatDB' })
public db: number;
#Column('varchar', { name: 'Name' })
public name: string;
#Column('datetime', { name: 'DDB_LAST_MOD' })
public ddbLastMod: Date;
}

LinkingObjects in react-native realm

I am currently trying to get the Realm Database for my react-native Application working, but I am unable to create a link between two Objects.
I am able to fetch Objects, I am able to create single Objects, but I am unable to link two Objects together
I followed the documentation, specifically I am looking at creating a Inverse Relationship
class PersonSchema {
public static schema: Realm.ObjectSchema = {
name: 'Person',
primaryKey: 'id',
properties: {
id: 'string',
username: 'string',
dogs: 'Dog[]',
},
};
}
class DogSchema {
public static schema: Realm.ObjectSchema = {
name: 'Dog',
primaryKey: 'id',
properties: {
id: 'string',
name: 'string',
owner: {type: 'linkingObjects', objectType: 'Person', property: 'dogs'},
},
};
}
I open my Realm with both schemas
componentDidMount() {
Realm.open({
schema: [PersonSchema.schema, DogSchema.schema],
deleteRealmIfMigrationNeeded: true,
}).then(realm => {
this.setState({realm});
});
}
One thing I tried was the following:
createPerson = () => {
const id = uuid();
const personenName = 'Steve ' + id;
const realm = this.state.realm;
if (realm) {
realm.write(() => {
const dog = realm.create('Dog', {
id,
name: 'Bello ' + id,
});
const dogs = realm.objects('Dog');
const person = realm.create('Person', {
id,
username: personenName,
dogs: [],
});
const persons = realm.objects('Person');
person.dogs.push(dog);
});
}
};
But I also tried linking the Dog to the Person. I tried creating a Dog and in that call create a Person for him and vice versa ... All tries throw an Error. The above Code just freezes the App.
How do I properly link two Objects together ?
For my App I want to link stuff to the User so I can look it up faster.

Mongodb - Create entry to a extisting collection field array

So I have a problem with my var array to add a new chapter, how would I go about this would I have to do this:
array.push({
chapter: [
{
id: 2,
title: 'adsf',
content: '',
authorNotes: 'asdf'
}
]
});
RiTest.ts
import * as mongoose from 'mongoose';
const Scheme = mongoose.Schema;
export const RiTestScheme = new Scheme({
novelName: String,
novelAuthor: String,
novelCoverArt: String,
novelTags: Array,
chapters: [
{
id: Number,
title: String,
content: String,
authorNotes: String
}
]
});
export class RiTestController {
public addChapter(callback: (data) => void) {
var chapterInfoModel = mongoose.model('ChaptersTest', RiTestScheme);
var array = [
{
chapter: [
{
id: 0,
title: 'prolog',
content: 'conetntt is empty',
authorNotes: 'nothing is said by author'
},
{
id: 1,
title: 'making a sword',
content: 'mine craft end chapter',
authorNotes: 'nothing'
}
]
}
];
let newChapterInfo = new chapterInfoModel(array);
newChapterInfo.save((err, book) => {
if (err) {
return callback(err);
} else if (!err) {
return callback(book);
}
});
}
}
This doesn't work, var array doesn't get saved into let newChapterInfo = new chapterInfoModel(array); what I am trying to do add another chapter to array.chapter but the array doesn't get recognized in the chapterInfoModel() how would I fix this array and add an item to the array to create a new entry into this existing collection
thank for taking your time to answer my question.
You are trying to insert array of document to your collections, That the reason its not inserting into your collection.
Document.prototype.save() will insert only one document to your collection depends on your definition. So to insert chapter here is the code below,
//array as Object
var array = {
chapter: [
{
id: 0,
title: 'prolog',
content: 'conetntt is empty',
authorNotes: 'nothing is said by author'
},
{
id: 1,
title: 'making a sword',
content: 'mine craft end chapter',
authorNotes: 'nothing'
}
]
};
//Push to your chapter array
array.chapter.push({
id: 2,
title: 'adsf',
content: '',
authorNotes: 'asdf'
});
let newChapterInfo = new chapterInfoModel(array);

How to iterate through nested object in Angular 2 using Type Script?

Hi I am developing web application using Angular 2. I am receiving JSON data using API. I am trying to segregate data. Below is my JSON data.
[
{
"userid":"f8b7b393-b36d-412b-82f7-9500e9eb6924",
"tenantid":"7a4a4ea9-3b39-4ef6-8d00-fcfe7454888c",
"username":"testuser3",
"emailaddress":"testuser3#rkdssravioutlook.onmicrosoft.com",
"isallowed":false,
"userroles":[
{
"userroleid":"c4c64675-ffe0-467b-87a4-00b899e0d48e",
"userid":"f8b7b393-b36d-412b-82f7-9500e9eb6924",
"roleid":"ada09fb2-fa83-4e46-8878-7e4e48c73111",
"tenantappid":1,
"validfrom":"2018-01-24T00:00:00",
"validto":"2018-01-24T00:00:00",
"isactive":true,
"isdeleted":false,
"role":{
"roleid":"ada09fb2-fa83-4e46-8878-7e4e48c73111",
"rolename":"Installer",
"tenantid":"99999999-9999-9999-9999-999999999999",
"isactive":true,
"isdeleted":false,
"actionnames":null,
"scopeids":null,
"scopes":null,
"actionids":null,
"actions":null
}
},
{
"userroleid":"bf632c7b-7540-479e-b8ec-b1471efd7f93",
"userid":"f8b7b393-b36d-412b-82f7-9500e9eb6924",
"roleid":"80dc8c6a-a934-4c2e-9d17-7cdd5b774fc6",
"tenantappid":1,
"validfrom":"2018-01-24T00:00:00",
"validto":"2018-01-24T00:00:00",
"isactive":true,
"isdeleted":false,
"role":{
"roleid":"80dc8c6a-a934-4c2e-9d17-7cdd5b774fc6",
"rolename":"Operator",
"tenantid":"99999999-9999-9999-9999-999999999999",
"isactive":true,
"isdeleted":false,
"actionnames":null,
"scopeids":null,
"scopes":null,
"actionids":null,
"actions":null
}
}
]
},
{
"userid":"8363def7-7547-425c-8d55-2116dd703cfc",
"tenantid":"7a4a4ea9-3b39-4ef6-8d00-fcfe7454888c",
"username":"testuser1",
"emailaddress":"testuser1#rkdssravioutlook.onmicrosoft.com",
"isallowed":false,
"userroles":[
{
"userroleid":"fe2b1f9f-4cd8-48dc-9708-2637e9743c1d",
"userid":"8363def7-7547-425c-8d55-2116dd703cfc",
"roleid":"ada09fb2-fa83-4e46-8878-7e4e48c73111",
"tenantappid":1,
"validfrom":"2018-01-24T00:00:00",
"validto":"2018-01-24T00:00:00",
"isactive":true,
"isdeleted":false,
"role":{
"roleid":"ada09fb2-fa83-4e46-8878-7e4e48c73111",
"rolename":"Installer",
"tenantid":"99999999-9999-9999-9999-999999999999",
"isactive":true,
"isdeleted":false,
"actionnames":null,
"scopeids":null,
"scopes":null,
"actionids":null,
"actions":null
}
}
]
},
{
"userid":"7f359233-5940-4b93-8ec9-fcf39e2fb58f",
"tenantid":"7a4a4ea9-3b39-4ef6-8d00-fcfe7454888c",
"username":"testuser2",
"emailaddress":"testuser2#rkdssravioutlook.onmicrosoft.com",
"isallowed":false,
"userroles":[
{
"userroleid":"c479b1c0-5275-40b2-893e-fc82dc55f1a5",
"userid":"7f359233-5940-4b93-8ec9-fcf39e2fb58f",
"roleid":"4dd2803b-e723-4356-8381-7c514ba13247",
"tenantappid":1,
"validfrom":"2018-01-24T00:00:00",
"validto":"2018-01-24T00:00:00",
"isactive":true,
"isdeleted":false,
"role":{
"roleid":"4dd2803b-e723-4356-8381-7c514ba13247",
"rolename":"Engineer",
"tenantid":"99999999-9999-9999-9999-999999999999",
"isactive":true,
"isdeleted":false,
"actionnames":null,
"scopeids":null,
"scopes":null,
"actionids":null,
"actions":null
}
}
]
}
]
Below are my corresponding models.
export class UserModel {
public userid: string;
public tenantid: string;
public isallowed: boolean;
public emailaddress: string;
public upn: string;
public userroles: UserRole[];
public roleid: string;
public isactive: boolean;
public tenantappid: string;
public username: string;
public userrolestext: string;
public validfrom: string;
public validto: string;
}
Below is role model
export class UserRole {
public userid: string;
public roleid: string;
public role: Role;
}
Below is the sample data i am trying to get
[
{
"userid":"f8b7b393-b36d-412b-82f7-9500e9eb6924",
"tenantid":"7a4a4ea9-3b39-4ef6-8d00-fcfe7454888c",
"rolename":"Installer",
"rolename":"Operator",
},
{
//rest of the data
}
]
First array of above object contains userid and below it contains again array of userroles. So i am trying to get each rolename associated with userid in a single row.
Below code i tried.
users.forEach(eachObj => {
eachObj.userroles.forEach(nestedeachObj => {
});
});
I am not able to go forward in the above foreach loop. Can someone help me to segregate above data? Any help would be appreciated. Thank you.
Hey I really don't know if my code example will achieve what you are looking for but what my example is creating looks like this:
RESULT:
[
{
tenantid: "7a4a4ea9-3b39-4ef6-8d00-fcfe7454888c",
userid: "f8b7b393-b36d-412b-82f7-9500e9eb6924",
rolename: "Operator"
},
{
tenantid: "7a4a4ea9-3b39-4ef6-8d00-fcfe7454888c",
userid: "8363def7-7547-425c-8d55-2116dd703cfc",
rolename: "Installer"
},
{
tenantid: "7a4a4ea9-3b39-4ef6-8d00-fcfe7454888c",
userid: "7f359233-5940-4b93-8ec9-fcf39e2fb58f",
rolename: "Engineer"
}
]
CODE:
const getRelevantData = (array) => {
data.forEach((user) => {
const obj = {};
obj.tenantid = user.tenantid;
obj.userid = user.userid;
user.userroles.forEach((userrole) => {
obj.rolename = userrole.role.rolename;
});
array.push(obj);
});
};
I have added below code and worked fine.
this.userroleData = [];
results.forEach(eachObj => {
eachObj.userroles.forEach(nestedeachObj => {
this.userroleData.push({
username: eachObj.username,
userrolestext: nestedeachObj.role.rolename,
});
});
});

Typescript Array - Splice & Insert

I have 3 arrays here of type channel array.
currentPickSelection: Array<channel>;
draggedChannel: channel;
droppedChannel: channel;
What I am trying to do is remove an item(the droppedChannel array item)from the currentPickSelection array and insert the draggedChannel item array onto the same index of the removed item.
Here is what I did so far, everything works except the insert part:
let index = this.currentPickSelection.findIndex(item => item === this.droppedChannel);
this.currentPickSelection.splice(index, 1, this.draggedChannel);
An this is the way I have the channel model declared:
export class CompChannel {
constructor(public compChannelCbsCode: string,
public compChannelName: string,
public compChannelLogo: string) {}
}
export class channel {
public pickCode: string;
public cbsCode: string;
public channel: string;
public logo: string;
public compChannel: CompChannel[];
constructor(pickCode: string, cbsCode: string, channel: string,
logo: string, compChannel: CompChannel[]) {
this.pickCode = pickCode;
this.cbsCode = cbsCode;
this.channel = channel;
this.logo = logo;
this.compChannel = compChannel;
}
}
Please advise what is wrong!
The droppedChannel object and the item found in currentPickSelection may not the exact same copy/clone of each other.
Try to compare with an unique value(like pickCode or cbsCode) in findIndex method instead of comparing with the whole object.
let currentPickSelection: any[] = [
{ id: 1, label: 'Test1' },
{ id: 2, label: 'Test2' },
{ id: 3, label: 'Test3' }
];
let draggedChannel: any = { id: 5, label: 'Test5' };
let droppedChannel: any = { id: 3, label: 'Test3' };
let index = currentPickSelection.findIndex(item => {
return item.id == droppedChannel.id; //use unique value instead of item object
});
if (index > -1)
currentPickSelection.splice(index, 1, draggedChannel);

Resources