I want to make a WYSIWYG editor on web, and it supports multiple users for inputting data, but while the user is editing, many divs will create during the process, and each div should have an unique id to write in database, and use JS to generate. But the question is.... the id should be unique, how can I ensure the js code is unique, seems the js is execute in the client side, it may not occur....(the user may modify it using firebugs), also, the other client is editing the same document also, they may also use the same id, and the system will have a problem.....
Any ideas on that?
Give each client a large unique number range.
Consider maintaining a global variable based on a session variable/application variable and increment it or else you can use random number / datetime or both of them together.
Maybe like this :
id = Application variable + Session variable + (increment as per requirement)...
That will serve in both ways to give a unique solution..
In fact with known increment, your server can keep track of all the changes you make on client.
hope that helps.
Related
What is the methodology for generating a unique ID that minimizes the chance for an overlap?
for(let i = 0; i < <Arbitrary Limit>; i++)
generateID();
There are a few current solutions, but they are all roundabout ways of solving the problem.
Possible Solution 1:
Use a database to generate the new ID.
This does not work for me since I would like to do this in the frontend.
Possible Solution 2:
Use Math.random()*Math.floor(LIMIT), where LIMIT is a numeric value.
This does not work as minimizing the chance for overlap requires a large limit, and thus a massive ID. If working with hundreds of thousands of instances that need an ID, the chance increases greatly.
Possible Solution 3:
'_' + Math.random().toString(36).substr(2, 9);
This is close to working but I believe Math.random() is pseudo-random.
Possible Solution 4:
Date.now(). Date.getTime(), etc.
This does not work as generating [Date.now(), Date.now()] will cause the same ID. It arguably also needs a long ID to minimize overlap.
I do not require absolutely 0% chance of generating the same ID, I wish to minimize the chance as much as possible without:
Storing a count
Using 'other technology' (no database, no library, etc)
Making a massive ID
This preferrably should be scalabe, eg. This should work for 10 or 1000000 IDs.
Edit: Unique IDs generated locally and without need for communication across users of the frontend. Ex: A component needs to render many instances of the same class and needs a key to assign to it. Keys must be different and upon unmounting the component with its generated keys/instances is removed.
It seems you want to generate unique IDs entirely on the front-end and without relying on a backend at all or "storing a count". Then the solution depends, in part, on how many different users you expect will access your application's frontend during its lifetime, and the size of IDs depends on how much you're willing to tolerate the risk of collisions (assuming you're generating IDs at random); for that, see Birthday problem.
Then, depending on the size of IDs you choose, you generate the IDs at random using a cryptographic RNG (such as the function crypto.randomBytes), which is the closest available to "truly" random IDs.
On the other hand, if only few users will access your front end, but each one generates many unique IDs, then you can assign each user a unique value from a central database, because then each front-end computer can use that unique value as part of unique identifiers it generates and ensure that the identifiers are unique across the application without further contacting other computers or the central database.
Notice that there are other considerations as well: You should also consider whether just anyone should access the resource an ID identifies simply by knowing the ID (even without being logged in or authorized in some way). If not, then additional access control will be necessary.
For your purposes, you can try applying sequential IDs. If sequential IDs alone are not adequate, then you can try applying a reversible operation on sequential IDs. One example of this operation is technically called a "linear congruential generator with a power of 2 modulus", such as the ones described in the following pages:
How to sync a PRNG between C#/Unity and Python?
Algorithm or formula that can take an incrementing counter and make it appear uniquely random
I need to keep a unique identifier for each client using my react app.
doing this will regenerate a random string (what I want) but does this on each refresh which is not what I want
const [id] = useState(Math.random().toString(36).substr(2, 8));
I've found uniqueId() form lodash but I'm afraid the id's won't be unique across multiple clients as it only give a unique Id and increment it at every call (1, 2, 3...)
const [id] = useState(_uniqueId());
Is there some kind of _uniqueId that generates a random string and also persist through page refresh?
I don't think there is a built-in or out-of-the-box solution that generates unique id in react that persist automatically. You have two problems to solve.
How to generate unique id. Which was already solved by using the uuid.
And how to persist it.
There are plenty of storage you can use depend on your need. Here's few of them where you can persist your data assuming you want it to be stored in client side.
LocalStorage
SessionStorage
Cookie
IndexedDB API
FileSystem
Again, it depends on your use case. So, carefully check them out which one fits on your requirement.
Another way to generate a temporary ID that would be the same for the same client, without storing it is to use browser fingerprinting.
For example, you can take user-agent, client timezone, and screen resolution, apply some hash function to them and call it an ID.
There are more advanced ways of fingerprinting that would result in less chance of two different users having the same ID, but it'll never be a 0% chance.
You also might want to use some libraries, such as https://github.com/fingerprintjs/fingerprintjs for this.
Does anyone know how to implement e.g. post counter to mongo db? I think I would do
accept /post with data
get mongo collection.count
add this custom id as {id: collection.count + 1}
but now I don't really know what will happen if 2 /posts will come at the same time. It will be queued in db? Or it will has 2 same fake id?
You can set the id field as unique to handle the case where 2 posts come at the same time...but, you will lose one document, since MongoDB won't allow another object for the same id.
You will have to write another piece of code to handle that case, which will be much more complex than a simple create operation.
Ideally, you shouldn't use a separate field in this way, unless it is required by your application and there is no alternative.
Here are a few caveats of using this approach:
For each post received, there are 2 DB operations being performed.
To prevent the loss of documents when the id field is set to unique, you will have to add another block of code, which might have to make a 3rd DB call to finally be stored in the database
Bottom line: Always use _id unless you have a reason not to do so.
I would like to create order number (following) in header, which would create automatically for each different opening the file by customer. Can I achieve this by using some functions in JS? or another? In attached screen this number should generate in each opening file
I presume that you are using Acrobat Pro to create the PDF form.
The quick and easy way to do this is to auto generate an order number based on the current date and time. Create a text field in your form (I've called mine "ordernumber"), double click it and go to the calculate tab then insert the following two lines into the custom calculation script box:
f = this.getField("ordernumber");
f.value = util.printd("yyyy/ddmm/hhmmss", new Date());
This will give you a unique order code (unless someone creates two orders in the same second!). You can change around the year (yyyy), day (dd), etc to make something that you like as a format.
If the order number needs to conform to an existing format or align with other systems then you would need to get the PDF to access an external database or something like that which would be a bit more complicated and beyond my knowledge.
It depends on whether your order number has to be unique only, or whether order numbers have to be consecutive.
In the first case, #Chris' answer pretty much gives the solution; you may be fiddling around with the base data, but that's it.
If the number has to be consecutive, there is a possibility if the use of the form can be limited to one single computer. In this case, you would create a Persistent Global Variable (which is a variable that is written back to the system, and can be reused the next time you open the document). See Acrobat JavaScript documentation for code samples. When you open the document, you read in that number, increment it and feed it into your order number field, and write it back.
If the number has to be consecutive, and the order form is used by several users, you will have to maintain the order number externally (which means, on a server). In this case, it might be even better to have a server-side order management, where the user may enter some base data, and then gets the prefilled order form made available.
Hopefully this isn't a redundant question--I'm not really sure how to word it. Let's say I have a list of items on an ASP.net page. That list is selectable in that whenever the user clicks on one, the page does a postback and the server code stores the index or some unique identifier of the picture in a ViewState property indicating that it is currently selected.
I would like to minimize the load on the server and therefore I would like to store the index or unique identifier representing the image in some way on the client side. The best way I can think to do this is to store said information in a hidden field ), however I had two questions about this before I go crazy:
Is this a security risk in any way, shape or form (i.e., exposing implementation details of the page)?
Is there a better/best way to do this that is more industry-standard? Does ASP.net provide a framework to do this that is cleaner than my idea? Seems like this would be a fairly common requirement to me...
I have been working in ASP.net for about two years now. Please, be kind :-)
Best,
Patrick Kozub
The only security risk would be if some list items must remain non-selectable. It sounds like that is not the case in your situation. The user already knows the information, because he or she already selected the item.
NOTE: If the server ever does anything with that information and pulls it back from the user, then you must validate the index value. Otherwise, the user could change it to an invalid index, such as (-2), and trigger an exception.
You can store the index (or related value) in a global javascript variable. If you're avoiding a trip back to the server, .NET doesn't really have a role to play.