Promise.all(sendData.franchisee.map(row => {
return knex('designer.settings').select('value').where({setting_key : 'PRICING_TIER'})
.then(pricing_tier => {
row.pricing_tier = pricing_tier[0].value;
knex('designer.pricing_tier').select('tier_title').where({id : row.pricing_tier})
.then(tier_title =>{
row.tier_title = tier_title[0].tier_title;
return row;
})
});
})).then(response => {
cb(sendData);
});
Hear it two query in promise 'designer.settings' and 'designer.pricing_tier'.
when execute 'designer.settings' i got that result in row after execute 'designer.pricing_tier' but that output not get in row. row.tier_title = tier_title[0].tier_title not in final sendData.
How sync both query in one promise?
Not sure if the query actually does exact same thing, but this merely demonstrates the basic idea how to do the above query correctly with knex.
Effectively the same thing with joining the pricing_tier to prevent need for 2 separate queries.
Promise.all(
sendData.franchisee.map(row => {
return knex('pricing_tier')
.withSchema('designer') // <-- selecting schema correctly in knex
// this join could have been done as a subquery in select too...
.join('settings', 'settings.setting_key', knex.raw('?', ['PRICING_TIER']))
.select('settings.value', 'pricing_tier.tier_title')
.where('pricing_tier.id', row.pricing_tier)
.then(rows => {
row.pricing_tier = rows[0].value;
row.tier_title = rows[0].tier_title;
return row;
});
})
).then(response => {
cb(sendData); // <--- generally bad idea to mix promises and callbacks
});
Resulting SQL is like this:
select
`settings`.`value`,
`pricing_tier`.`tier_title`
from `designer`.`pricing_tier`
inner join `designer`.`settings` on `settings`.`setting_key` = 'PRICING_TIER'
where `pricing_tier`.`id` = 3219
You should wrap both queries into a promise which resolves only when both query have finished the job like this:
Promise.all(sendData.franchisee.map(row => {
return new Promise((resolve) => {
knex('designer.settings').select('value').where({setting_key : 'PRICING_TIER'})
.then(pricing_tier => {
row.pricing_tier = pricing_tier[0].value;
knex('designer.pricing_tier').select('tier_title').where({id : row.pricing_tier})
.then(tier_title =>{
row.tier_title = tier_title[0].tier_title;
resolve(row);
})
});
});
})).then(response => {
cb(sendData);
});
Related
I need to check if entry with specific ID exists in the database using Sequelize in Node.js
function isIdUnique (id) {
db.Profile.count({ where: { id: id } })
.then(count => {
if (count != 0) {
return false;
}
return true;
});
}
I call this function in an if statement but the result is always undefined
if(isIdUnique(id)){...}
I don't prefer using count to check for record existence. Suppose you have similarity for hundred in million records why to count them all if you want just to get boolean value, true if exists false if not?
findOne will get the job done at the first value when there's matching.
const isIdUnique = id =>
db.Profile.findOne({ where: { id} })
.then(token => token !== null)
.then(isUnique => isUnique);
Update: see the answer which suggests using findOne() below. I personally prefer; this answer though describes an alternative approach.
You are not returning from the isIdUnique function:
function isIdUnique (id) {
return db.Profile.count({ where: { id: id } })
.then(count => {
if (count != 0) {
return false;
}
return true;
});
}
isIdUnique(id).then(isUnique => {
if (isUnique) {
// ...
}
});
You can count and find.
Project
.findAndCountAll({
where: {
title: {
[Op.like]: 'foo%'
}
},
offset: 10,
limit: 2
})
.then(result => {
console.log(result.count);
console.log(result.rows);
});
Doc link, v5 Beta Release
I found the answer by #alecxe to be unreliable in some instances, so I tweaked the logic:
function isIdUnique (id, done) {
db.Profile.count({ where: { id: id } })
.then(count => {
return (count > 0) ? true : false
});
}
As Sequelize is designed around promises anyway, alecxe's answer probably makes most sense, but for the sake of offering an alternative, you can also pass in a callback:
function isIdUnique (id, done) {
db.Profile.count({ where: { id: id } })
.then(count => {
done(count == 0);
});
}
}
isIdUnique(id, function(isUnique) {
if (isUnique) {
// stuff
}
});
Extending #Jalal's answer, if you're very conscious about performance implications while maintaining a simple Sequelize structure and you do not need the row data, I suggest you only request one column from the database. Why waste bandwidth and time asking the database to return all columns when you won't even use them?
const isIdUnique = id =>
db.Profile.findOne({ where: { id }, attributes: ['id'] })
.then(token => token !== null)
.then(isUnique => isUnique);
The attributes field tells Sequelize to only request the id column from the database and not sending the whole row's content.
Again this may seem a bit excessive but at scale and if you have many columns that hold a lot of data, this could make a giant difference in performance.
Try the below solution. I tried it and it works well.
const isIdUnique = async (id, model) => {
return await model.count({ where: { id: id } });
};
const checkExistId = await isIdUnique(idUser, User);
console.log("checkExistId: ", checkExistId);
From the docomuntation of ngx-Spinner said in FEAUTERS: "show()/hide() methods return promise",
but in my Project in intellij:
this.spinner.show() returns void.
ane therefore when im trying to do this:
onCheckOut() {
this.spinner.show().then(p => {
this.cartService.CheckoutFromCart(1);
});
}
im getting the : ERROR in src/app/components/checkout/checkout.component.ts:33:25 - error TS2339: Property 'then' does not exist on type 'void'.
33 this.spinner.show().then(p => {
how i make it work?
Maybe the documentation is wrong. When I look at the code, it does not return a promise:
show(name: string = PRIMARY_SPINNER, spinner?: Spinner) {
setTimeout(() => {
const showPromise = new Promise((resolve, _reject) => {
if (spinner && Object.keys(spinner).length) {
spinner['name'] = name;
this.spinnerObservable.next(new NgxSpinner({ ...spinner, show: true }));
resolve(true);
} else {
this.spinnerObservable.next(new NgxSpinner({ name, show: true }));
resolve(true);
}
});
return showPromise;
}, 10);
}
Not sure if this was done on purpose or not, but you could raise an issue on Github.
The only way to achieve your solution would be to use the getSpinner method of the service:
const spinnerName = "my-spinner";
this.spinner.show(spinnerName);
this.spinner.getSpinner(spinnerName)
.pipe(
first(({show}) => !!show)
)
.subscribe(() => {
this.cartService.CheckoutFromCart(1);
}
);
Here is my problem.
I'm running a method that sends me a json (method = myTableService.getAllTables ()), to create an object (object = this.myTables).
Then I execute the method for each, for each element of this.myTables I execute a new request (request = this.myTableService.getTableStatut (element.theId)).
I retrieve data from a new json to create an object (object = myTableModel).
Each result will be added to this.myTableListProvisory.
The problem is the order of execution.
It execute the console.log before the end of the for each...
This.myTableListProvisory.length and this.myTableList.length return 0.
How to wait for the end of the for each run before running the console.log?
Thank you
ngOnInit() {
this.myTableService.getAllTables()
.subscribe(data => {
this.myTables = data;
this.myTableList = this.getAllTableStatut(this.myTables);
console.log("this.myTableList.length : " + this.myTableList.length);
}, err => {
console.log(err);
})
}
getAllTableStatut(myTables: any) {
this.myTableListProvisoire = [];
myTables.forEach(element => {
this.myTableService.getTableStatut(element.theId)
.subscribe(data => {
this.statut = data;
this.myTableModel = new MyTableModel(element.tableNumber, this.statut.name, element.theId);
this.myTableListProvisoire.push(this.myTableModel);
})
console.log("this.myTableListProvisoire.length : " + this.myTableListProvisoire.length);
})
return this.myTableListProvisoire;
}
Result of console.log
this.myTableListProvisoire.length : 0
this.myTableList.length : 0
UPDATE
I have simplified the code ... I put it in its entirety for the understanding. What I need is to sort the array after it is done. The problem is that I don't know how to use a flatMap method in a query inside a foreach ... I have temporarily placed the sort method inside the subscribe which is a bad solution for the performance. That's why I want to do my sort after the creation of the array. Thank you
export class MyTableComponent implements OnInit {
myTables: any;
statut: any;
myTableModel: MyTableModel;
myTableList: Array<MyTableModel>;
myTableListProvisoire: Array<MyTableModel>;
i: number;
j: number;
myTableModelProvisoire: MyTableModel = null;
constructor(public myTableService: MyTableService) { }
ngOnInit() {
this.myTableService.getAllTables()
.subscribe(data => {
this.myTables = data;
this.myTableList = this.getAllTableStatut(this.myTables);
}, err => {
console.log(err);
})
}
getAllTableStatut(myTables: any) {
this.myTableListProvisoire = [];
myTables.forEach(element => {
this.myTableService.getTableStatut(element.theId)
.subscribe(data => {
this.statut = data;
this.myTableModel = new MyTableModel(element.tableNumber, this.statut.name, element.theId);
this.myTableListProvisoire.push(this.myTableModel);
for (this.j = 0; this.j < this.myTableListProvisoire.length; this.j++) {
for (this.i = 0; this.i < this.myTableListProvisoire.length - 1; this.i++) {
if (this.myTableListProvisoire[this.i].getTableNumber() > this.myTableListProvisoire[(this.i + 1)].getTableNumber()) {
this.myTableModelProvisoire = this.myTableListProvisoire[this.i];
this.myTableListProvisoire[this.i] = this.myTableListProvisoire[(this.i + 1)];
this.myTableListProvisoire[(this.i + 1)] = this.myTableModelProvisoire;
}
}
}
}, err => {
console.log(err);
})
}, err => {
console.log(err);
})
return this.myTableListProvisoire;
}
}
Well Observables are asynchronous actions and will be executed after finishing the current execution block. So when the js engine comes to your
this.myTableService.getTableStatut(element.theId)
.subscribe(data => {
this.statut = data;
this.myTableModel = new MyTableModel(element.tableNumber, this.statut.name, element.theId);
this.myTableListProvisoire.push(this.myTableModel);
})
it will only create a subscription, but the code inside of it will be executed after all the other code in the block. So that's why your console.log is being executed before you get any data. So you need to place it inside the .subscribe block to see the. I think there can be a better solution to get the data, but I don't know the structure of the app, so I can't advice. If you create an example on https://stackblitz.com/ I could probably help you out with a better solution.
I try to create a random number with following code
let randomnumber = (Math.random().toString().slice(-8))
And I will check if the mysql table has this random number, if doesn't, this number will be inserted into the table, if do, run the above code again, and check again. like this
await mysqlModel.checkNumberExit([randomnumber])
.then(async(results) => {
if (results.length === 0) {
//doesn't exist, do something
} else {
//exist, repeat until the random number doesn't exist
}
Here is my question, how can I do this function efficiently, this way I am using is very low efficiency, any ideas?
You can use a function to generate and check:
function getRandomNumber() {
let randomnumber = Math.floor(Math.random(1000,9999)*100000000);
mysqlModel.find({"fieldName":randomnumber})
.then(res => {
if(!res) {
//doesn't exist, do something
}else{
getRandomNumber();
}
}).catch(err => {
//err
});
}
call the function using:
getRandomNumber()
Product (id, currentdraw, totaldraw, ratio)
Product is my table
I want to use currentdraw and totaldraw to update ratio
Here is my code
Product.updateRatio = function(pid, cb) {
db('product')
.where({
productid : pid
})
.select('currentdraw', 'totaldraw')
.then(function(ratiolist) {
db('product')
.where({
productid : pid
})
.update({
ratio : ratiolist.currentdraw / ratiolist.totaldraw
})
cb(null, ratiolist);
})
.catch(function(err) {
cb(new GeneralErrors.Database());
console.log('updateRatio');
});
}
When I run the code, no error occuring but ratio column doesn't update.
I don't know where is wrong.
Can someone help me? Thanks
I suppose that your ratiolist is array instead of an object, add console.dir(ratiolist) to check what is returned from the first query:
function(ratiolist) {
// here ratiolist is an array
db('product')
.where({
productid : pid
})
.update({
// undefined / undefined is NaN
ratio : ratiolist.currentdraw / ratiolist.totaldraw
})
// second query is not yet ran when this callback is called
// try to use promise chains...
cb(null, ratiolist);
})
Better way to do this query would be:
// return value passed as promise instead of callback
Product.updateRatio = function(pid) {
return db('product')
.where('productid', pid)
.update({
ratio : db.raw('?? / ??', ['currentdraw', 'totaldraw'])
})
.then(() => db('product').where('productid', pid));
}
If you insist using callbacks this should work:
Product.updateRatio = function(pid, cb) {
db('product')
.where('productid', pid)
.update({
ratio : db.raw('?? / ??', ['currentdraw', 'totaldraw'])
})
.then(() => db('product').where('productid', pid))
.then(ratiolist => cb(null, ratiolist));
}