While doing my first Angular 2 CRUD exercise, I am trying to re-use my edit form to both add new and edit an existing object.
I am having this improper code:
this._route.params.forEach((params: Params) => {
let id = params['id'];
if (id) {
this._productService.getProduct(id)
.subscribe(
product => this.product = product,
error => this.errorMessage = <any>error
);
} else {
this.product = {
name: 'default name',
ean: '',
price: 0,
qty: 0,
imageUrl: '',
_id: ''
};
}
});
(<FormGroup>this.registerForm).setValue(this.product, {onlySelf: true});
... which no wonder does not work - because one part of the if is sync, the other - async.
I want to optimize the code and convert the static default object template into a value from an immediately resolved Observable - basically an equivalent of $q.when(myVar). How do I do that?
Now I know it'd be:
return Observable.of({
name: 'default name',
ean: '',
price: 0,
qty: 0,
imageUrl: '',
_id: ''
});
Related
In a react-native project using Realm-js, I've just created a clone of the app, integrated all libs, and copied over all src directories.
The app builds installs and runs on Android.
When i go through the authentication flow (which utilizes realm to store auth data), i ultimately get an error:
[ Error: RealmObject cannot be called as a function ]
login function:
async function login(username, password) {
try {
const result = await Api.login({
username: username,
pass: password,
});
const userAuthResult = await Db.updateAuth(result);
setUserAuth(userAuthResult);
} catch (err) {
console.log('[ ERROR ]:', err)
if (!err.message || err.message.includes('Network Error')) {
throw new Error('Connection error');
}
throw new Error('Wrong username or password');
}
}
and ive narrowed down the issue to Db.updateAuth(...)
updateAuth:
export const updateAuth = (params) => {
console.log(' [ HERE 1 ]')
const auth = {
id: params.id,
token: params.token,
refreshToken: params.refresh_token,
tokenExpiresAt: Math.floor(Date.now() / 1000) + 600, //params.expires_at,
federatedToken: params.federatedToken ?? '',
federatedTokenExpiresAt: params.federatedTokenExpiresAt ?? 0,
username: params.username,
name: params.name,
roleName: params.role_name,
roleId: params.role_id,
lastLogin: Math.floor(Date.now() / 1000),
};
console.log(' [ HERE 2 ]')
realm.write(() => {
console.log(' [ HERE 3 ]')
realm.create('Authorizations', auth, 'modified'); // PROBLEM
});
return auth;
};
inspecting the schema, i found theres no federatedToken propereties, yet in the auth update object, there are two. not sure why it wouldnt be throwing an error in the original non-cloned app.
authorizations schema:
AuthorizationsSchema.schema = {
name: 'Authorizations',
primaryKey: 'id',
properties: {
id: 'int',
token: 'string',
refreshToken: 'string',
tokenExpiresAt: 'int',
username: 'string',
name: 'string',
roleName: 'string',
roleId: 'int',
lastLogin: 'int',
},
};
Realm.js (class declaration) -> https://pastebin.pl/view/c903b2e2
from realm instantiation:
let realm = new Realm({
schema: [
schema.AccountSchema,
schema.AuthorizationsSchema,
schema.AvailableServiceSchema,
schema.FederatedTokensSchema,
schema.NoteSchema,
schema.PhotoSchema,
schema.PhotoUploadSchema,
schema.PrintQueueSchema,
schema.ProductSchema,
schema.ReportSchema,
schema.ServicesSchema,
schema.UploadQueueJobSchema,
schema.InvoicesSchema,
schema.TestSchema
],
schemaVersion: 60,
deleteRealmIfMigrationNeeded: true,
//path: './myrealm/data',
});
this logs the 1, 2, and 3 statements. The issue seems to come from the 'problem' line. Im not sure what exactly this error means, as there doesnt seem to be anything in realm's repo about it, and in the app this was cloned from, there was no issue with this line. I can also see other lines are throwing similar errors later on the user flows
Anyone know what this is about? or where i can learn more?
React-native: v64.2
realm-js: 10.6.0 (app cloned from was v10.2.0)
MacOS: 11.3 (M1 architecture)
in order to create you have the first call, the realm.write a method like this.
const storeInDataBase = (res,selectedfile) => {
try{
realm.write(() => {
var ID =
realm.objects(DocumentConverstionHistory).sorted('HistoryID', true).length > 0
? realm.objects(DocumentConverstionHistory).sorted('HistoryID', true)[0]
.HistoryID + 1
: 1;
realm.create(DocumentConverstionHistory, {
HistoryID: ID,
Name:`${selectedfile.displayname}.pdf`,
Uri:`file://${res.path()}`,
Date: `${new Date()}`
});
})
}catch(err){
alert(err.message)
}
}
Here is the schema file
export const DATABASENAME = 'documentconverter.realm';
export const DocumentConverstionHistory = "DocumentConverstionHistory"
export const DocumentConverstionHistorySchema = {
name: "DocumentConverstionHistory",
primaryKey: 'HistoryID',
properties: {
HistoryID: {type: 'int'},
Name: {type: 'string'},
Uri: {type: 'string?'},
Type: {type: 'string?'},
Size: {type: 'string?'},
Date: {type: 'date?'}
}
};
I'm developing flutter app with FireStore.
I have declared a variable 'total' and List of objects to be filled with some data from collection called 'cart', then upload these data to collection called 'orders'
This code grab the data from collection 'cart':
void submitOrder() async {
List<CartItem> products =[];
double total =0;
CartItem temp= CartItem(customerId:'' ,itemId:'' ,id: '', title: '', quantity:0, price: 0);
DatabaseService().cartCollection.getDocuments().then(
(snapshot) => snapshot.documents.forEach((doc) {
temp = CartItem(
customerId: user.uid,
itemId: doc.data['itemId'].toString(),
id: doc.documentID.toString(),
title: doc.data['itemName'].toString(),
quantity: int.tryParse(doc.data['quantity'].toString()) ,
price: double.tryParse( doc.data['price'].toString()));
total += temp.quantity*temp.price;
print(total); /// This print shows accurate total
products.add(temp);
}
),
);
// Send data to setOrderData in db class to set new doc in order collection
DatabaseService().setOrderData(products, total, user.uid, branchId, 'open');
}
method setOrderData in DB class:
// Add Order
Future setOrderData(List<CartItem> cartProducts, double total, String customerId, String branchId, String status ) async {
final timestamp = DateTime.now();
return await orderCollection.document(uid).setData(
{
'customerId': customerId,
'branchId': branchId,
'status': status ,
'amount': total.toString(), //
'dateTime': timestamp.toIso8601String(),
'products': cartProducts.map((cp) => {
'id': cp.id,
'title': cp.title,
'quantity': cp.quantity,
'price': cp.price,
}).toList(),
});
}
data in FireStore shows that products and total are null?
Link to image:
If anyone can help me out I'd be grateful.
I highly recommend using a code formatter on your code, as it makes it much more likely that people can spot problems like the one you're having. When I reformat the first code block you shared, it becomes:
void submitOrder() async {
List<CartItem> products = [];
double total = 0;
CartItem temp = CartItem(
customerId: '', itemId: '', id: '', title: '', quantity: 0, price: 0);
DatabaseService().cartCollection.getDocuments().then(
(snapshot) => snapshot.documents.forEach((doc) {
temp = CartItem(
customerId: user.uid,
itemId: doc.data['itemId'].toString(),
id: doc.documentID.toString(),
title: doc.data['itemName'].toString(),
quantity: int.tryParse(doc.data['quantity'].toString()),
price: double.tryParse(doc.data['price'].toString()));
total += temp.quantity * temp.price;
print(total);
/// This print shows accurate total
products.add(temp);
}),
);
// Send data to setOrderData in db class to set new doc in order collection
DatabaseService().setOrderData(products, total, user.uid, branchId, 'open');
}
An in this format it's immediately clear to me that you're calling setOrderData before any of the products.add(temp) calls have happened.
This is because data is loaded from Firestore asynchronously. Since this may take some time, your main code (including the return) continues so the UI is not blocked. Then when the data is available, your then callback is invoked.
This means that any code that needs the data from Firestore needs to be inside the then callback. So in your case, the solution could be as simple as moving the call to setOrderData to inside the then:
void submitOrder() async {
List<CartItem> products = [];
double total = 0;
CartItem temp = CartItem(
customerId: '', itemId: '', id: '', title: '', quantity: 0, price: 0);
DatabaseService().cartCollection.getDocuments().then(
(snapshot) => snapshot.documents.forEach((doc) {
temp = CartItem(
customerId: user.uid,
itemId: doc.data['itemId'].toString(),
id: doc.documentID.toString(),
title: doc.data['itemName'].toString(),
quantity: int.tryParse(doc.data['quantity'].toString()),
price: double.tryParse(doc.data['price'].toString()));
total += temp.quantity * temp.price;
print(total);
/// This print shows accurate total
products.add(temp);
}),
// Send data to setOrderData in db class to set new doc in order collection
DatabaseService().setOrderData(products, total, user.uid, branchId, 'open');
);
}
So the things to take away from this:
Always format your code, as it makes it easier for you and others to understand the flow and find problems.
Data is loaded from Firestore (and most cloud APIs) asynchronously, and you can only use the data inside the then() callback, or by using await.
The best Practice to solve such an error is to use try and then catch with the await for each function like the below code
void submitOrder() async {
List<CartItem> products = [];
double total = 0;
CartItem temp = CartItem(
customerId: '', itemId: '', id: '', title: '', quantity: 0, price: 0);
try {
await DatabaseService().cartCollection.getDocuments().then(
(snapshot) => snapshot.documents.forEach((doc) {
temp = CartItem(
customerId: user.uid,
itemId: doc.data['itemId'].toString(),
id: doc.documentID.toString(),
title: doc.data['itemName'].toString(),
quantity: int.tryParse(doc.data['quantity'].toString()),
price: double.tryParse(doc.data['price'].toString()));
total += temp.quantity * temp.price;
print(total);
/// This print shows accurate total
products.add(temp);
}),
);
// Send data to setOrderData in db class to set new doc in order collection
await DatabaseService()
.setOrderData(products, total, user.uid, branchId, 'open');
} catch (error) {
print(error);
}
}
i create comment in WordPress with REST API. But can't set comment meta. I don't know why, maybe i missing a little bit. So please help.
The code:
// Data
var ajax_data = {
author: user.user_id,
author_email: user.user_email,
content: comment,
parent: parent,
post: post_id,
meta: {
'_crating_speed': rating_speed,
'_crating_price': rating_price,
},
}
// Set header
$.ajaxSetup({
headers: {
'Authorization': "Bearer " + user.token,
}
});
// AJAX
$.post( rest_base + '/comments', ajax_data ).done( function( response ) {
console.log(response);
}).fail( function( xhr, status, error ) {
console.log( error);
}, 'json' );
The meta _crating_speed and _crating_price not inserted to database.
Same issue, solution is to add these using register_meta() which will add the meta fields to the schema of the comment.
https://developer.wordpress.org/reference/functions/register_meta/
In my case the following worked:
add_action('init', function() {
register_meta('comment', 'stars', [
'type' => 'number',
'description' => __('Stars'),
'single' => true,
'show_in_rest' => true
]);
}, 10 , 0);
I am looking for a way to retrieve all validation errors. (I'm using Collection2 and SimpleSchema)
Consider this code:
Foo.insert({
title: '',
description: ''
}, function(error, result) {
console.error(error);
});
output:
{
message: 'Title may not be empty.',
invalidKeys: [
0: {
name: 'title',
type: 'required',
value: ''
},
1: {
name: 'description',
type: 'required',
value: ''
}
]
}
I would like to have all the error messages that are related to validation.
Unfortunately I couldn't find any solution for this.
SOLUTION:
I've found a satisfiable solution
Foo.simpleSchema().namedContext().keyErrorMessage('title');
I ran into the same problem and my solution was to insert said errors into a client mongo error collection which would then display the errors to the user. The following is what I came up with:
Schema
Schema.newUser = new SimpleSchema({....});
Client Side Validation
function tokenRegistration (newUser) {
var valContext = Schema.newUser.namedContext('tokenRegForm');
if (!valContext.validate(newUser)) {
var keys = valContext.invalidKeys();
_.each(keys, function (value) {
var error = value.name,
message = valContext.keyErrorMessage(error);
return ErrorMessage.insert({errormessage: message})
});
}
}
I have a project in symfony that I would like to let my users upload an image for their "avatar" field. I have found many posts about how to "extend" the table which I have with the schema below:
Member:
inheritance:
type: column_aggregation
extends: sfGuardUser
columns:
idmember: { type: integer }
birthday: { type: date }
avatar: { type: string(255) }
bio: { type: string(255) }
The columns get added to the table just fine, but when I go to change the widget to a sfWidgetFormInputFileEditable it breaks. Here is the Form.class file:
$file_src = $this->getObject()->getAvatar();
if ($file_src == '')
{
$file_src = 'default_image.png';
}
$this->widgetSchema['avatar'] = new sfWidgetFormInputFileEditable(array(
'label' => ' ',
'file_src' => '/uploads/avatar/'.$file_src,
'is_image' => true,
'edit_mode' => true,
'template' => '<div>%file%<br />%input%</div>',
));
and "save" function of the form:
if($this->isModified())
{
$uploadDir = sfConfig::get('sf_upload_dir');
$thumbnail = new sfThumbnail(150, 150);
$thumbnail2 = new sfThumbnail(800, 800);
if($this->getAvatar())
{
$thumbnail->loadFile($uploadDir.'/avatar/'.$this->getAvatar());
$thumbnail->save($uploadDir.'/avatar/thumbnail/'. $this->getAvatar());
$thumbnail2->loadFile($uploadDir.'/avatar/'.$this->getAvatar());
$thumbnail2->save($uploadDir.'/avatar/big/'. $this->getAvatar());
}
}
When I submit the form, I get this error message:
This form is multipart, which means you need to supply a files array as the bind() method second argument.
In the action where you bind the form you should use something like this:
$form->bind($request->getParamater($form->getName()), $request->getFiles($form->getName()));
So you need to pass the uploaded files as the second parameter to the bind method.