How can I check if a doc exists in firebase? [closed] - reactjs

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
How can I check if an id it's inside my firebase collection?
I need to check if item.id it's inside the collection "favorites" that contains multiple ids.
I need to assign to a value true if it's inside or false if there is not a value with the same id.

As far as I know, there is no way to "check" if a document exists inside a collection without actually fetching it. When you attempt to get the document, it returns a doc - with a exists variable.
So you can check if a document exists or not like this (using Promises):
const doesDocExist = (docID) => {
return firestore().collection("cities").doc(docID).get().then((doc) => {
return doc.exists
})
}
//Call the function to check if doc exists:
let doesSanFranciscoExist = await doesDocExist("SF")
Note that in the above function, you are attempting to fetch the document with the ID.
Alternatively, if you want to check the existence of multiple documents with just one read, you can maintain a list of document ids elsewhere in your database using Cloud Functions.
Get a document in Firestore

Related

How to fill an array from server answer with async pipes [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I'm new to Angular, and not so new to ASP.NET Core. I'm trying to retrieve data from backend server (ASP.NET Core & SQL Server Express & Dapper ORM). GET request passing valid ([first screen][1]), and now I want to map data from it to HTML view.
So a valid JSON is returned, and I'm trying to pass its values via async pipe. Correct ([second screen][2]). Then why can't I fill an array from other services and other GETs like on [third screen][3] and map them to array too like on [fourth screen][4]?
They're undefined when I'm trying to retrieve data from them like [that][5]. What's the problem and what RxJS expression do I need?
The async pipe will only work on observable sources. The GET request produces an observable, so async works.
If your other services are returning arrays (not arrays wrapped in an observable), async won't work and isn't necessary, anyway.
E.g.
const directArray: string[] = ["this", "that", "other"];
you can use *ngFor="let thing of directarray"
const observableArray: Subject<string[]> = new Subject();
observableArray.next(["this", "that", "other"]);
you can use *ngFor="let thing of observableArray | async"

Toggle boolean value using hooks in React [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
Taking my first pass using hooks and running into some issues toggling a boolean value. I can toggle the style when clicking "complete" on an individual item but when I click another "complete" on another item it doesn't fire until you click for the second time.
Below is sandbox URL to help explain what I am running into.
https://codesandbox.io/s/react-to-do-with-hooks-mpqfl
You don't need status to achieve what you are trying to do here.
Here is an updated sandbox which works as expected https://codesandbox.io/s/react-to-do-with-hooks-tvyg2
Your sandbox doesn't work as you are using only 1 variable status to control all your To-Do items.
To explain it better, say there are 2 To-Dos:
1.Learn react
2.Meet friend for lunch
The default value of status is false at first then you mark the first to do as completed and set the value of status as true.
1.Learn react
2.Meet friend for lunch
Everything works as expected as of now.
Now the value of status is true
When you try to mark the 2nd to do as completed with the code here:
newTodos[index].isCompleted = !status;
isCompleted is again assigned false i.e. not completed as !status is false at this point. So no changes occur in your To-Do list.
Following this line you again set status as false here.
setStatus(!status);
Now the new value of status is false.
So when you click on the same To-Do the 2nd time, isCompleted is set to !status i.e true and the list changes to:
1.Learn react
2.Meet friend for lunch
As you were using only 1 variable to track the status of all To-Dos you were facing the observed behaviour
i.e you had to click the button twice to mark it as complete

SqlException: Invalid column name 'CreateRequestViewModelRequestId' [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I got this error when I tried to save a Response model object into the database by executing this line:
_context.Add(response);
await _context.SaveChangesAsync(); <-- Exception happens in here
The RequestId column in Response table is a foreign key referenced from Request table.
CreateRequestViewModel is the view model I created based on Request model but only have the fields required user to fill in to create a Request.
CreateResponseViewModel is the view model I created based on Response model but only have the fields required user to fill in to create a Response.
The database models are generated using command:
Scaffold-DbContext "Data Source=localhost;Initial Catalog=Test;Integrated Security=True" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models
I mapped the value from CreateResponseViewModel object to Response model object in controller action method and tried to save into database, but got this error. My guessing it may be related to cascading update/insert in the Entity Framework Core, but have no idea how to fix it.
Any help would be appreciated. Thank you.
Generally speaking, that error occurs when your entities are out of sync with your database. Based on the current state of your entity classes, Entity Frameworks thinks there should be a column named CreateRequestViewModelRequestId, but that column does not actually exist in the table. Short answer is that you need to run a migration to update the database.
However, it's not at all clear what you're doing here and nothing with ViewModel at the end of the class name should ever be going into your database. I imagine you have much deeper issues here that will need to be solved.
The problem is caused by adding these two lines in the Context class:
public virtual DbSet<CreateRequestViewModel> CreateRequest { get; set; }
public virtual DbSet<CreateResponseViewModel> CreateResponse { get; set; }
Chris is right, ViewModel should not go into database. After remove these 2 lines, it works as expected.

Trouble capturing all parameters [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
Ok, I am trying to make a record and send an email notification/acknowledgement that the record has been recorded. I have a many to many relationship between a Players model and a Lessonhours model. The User model has a one to many with the Players model.
I have run my 'store' method with several different modifications and I finally get my email to send. The problem is that I can't get the array of players that exist and send each an email. The multiple selections from my form are being properly inserted in their respective tables. When it comes to collecting the email data, I have come closest with the following code. The problem with THIS is that I only get one player instead of two or more when they exist. I hope this makes sense. Screenshots below.
Code and screenshot of $request array:
Players Model:
I am not very experienced with this and I am finding it difficult to pinpoint which documentation example to use. How can I get all of the emails addresses for sending after the insertion of record? I appreciate all help offered.
Lessonhours Model:
Store Lessonhours form
User Model
I'm not sure why you're putting an array inside an array for whereIn clause but if request players is already coming through as an array then your should just be able to do:
whereIn('id', $request->players)
Next you're only ever going to be sending one email because you're returning from inside the loop so it's going to send the first player one email and then return a response. To save have a temporary variable that you're not using again you could do:
Players::with('users')->whereIn('id', $request->players)->get()
->each(function ($player) use ($lesshours) {
Mail::to($player->users)->send(new ThankYouForLessonPackagePurchase($lesshours));
});
return back()->with(['success' => 'Player is enrolled in new Lesson Package.']);
You don't have to do the above it's just another way to write it.
Lastly, if you're always going to require player ids to be sent then you might as well add it to the validation array:
'players => 'required',
'players.*' => 'exists:players,id',
Hope this helps!

How to migrate from Yii to Woocommerce Wp, Still keep to old database (table,rows) [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 9 years ago.
Improve this question
I have an e-commerce website writen by yii framework. I want to rebuild with woocommerce on wordpress. And I want to keep old database (all table, rows). But wordpress save data on a different way (metadata).
what is solution in this case, how to import old database and custom field same my old database?
P/s: thanks for advance and sorry about my english.
Unfortunately, there is no easy way to do this:
First, you must create the post using wp_insert_post:
wp_insert_post( $post, $wp_error );
http://codex.wordpress.org/Function_Reference/wp_insert_post
Second, you must add the appropriate metadata using add_post_meta:
add_post_meta($post_id, $meta_key, $meta_value, $unique);
http://codex.wordpress.org/Function_Reference/add_post_meta
What you end up with is something like this psuedo-code:
$yii_products = (get products from yii db. Perhaps init a new PDO connection to connect to that db.)
foreach($yii_products as $product)
{
//Create post array from existing datatype in yii
$post = array(
'title' => $product->title,
...
);
//Save post
$post_id = wp_insert_post($post);
//Add metadata
//some_yii_field_name could be 'color', 'size' or whatever woocommerce uses
add_post_meta($post_id, 'some_yii_field_name', $product->some_yii_field_name);
}
This will create all of your new post types and with the appropriate metadata. I would put this code into a throw-away plugin used only for migration.
Good luck!

Resources