couchdb 设计视图,更新文档创建字段

发布于 2024-09-24 16:36:05 字数 471 浏览 2 评论 0原文

创建/更新文档时是否可以动态更新沙发或更改字段?例如,在设计视图中... validate_doc_update:

function(newDoc, oldDoc, userCtx) {
}

在该函数中,我可以抛出如下错误:

if(!newDoc.user_email && !newDoc.user_name && !newDoc.user_password){
    throw({forbidden : 'all fields required'});
}

我的问题是如何重新分配字段?我尝试了这个:

newDoc.user_password ="changed";

更改为一些新值或哈希值。我的总体目标是用node和couchdb构建一个用户注册/登录系统,但还没有找到很好的例子。

Is it possible to have couch update or change fields on the fly when you create/update a doc? For example in the design view.... validate_doc_update:

function(newDoc, oldDoc, userCtx) {
}

Within that function I can throw errors like:

if(!newDoc.user_email && !newDoc.user_name && !newDoc.user_password){
    throw({forbidden : 'all fields required'});
}

My Question is how would I reassign a field? I tried this:

newDoc.user_password ="changed";

with changed being some new value or hashed value. My overall goal is to build a user registration/login system with node and couchdb and have not found very good examples.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

九命猫 2024-10-01 16:36:05

validate_doc_update 函数不能有任何副作用,并且不能在存储之前更改文档。它仅有权阻止更新或允许更新通过。这很重要,因为该函数不仅在用户请求更新时调用,而且在将更改从一个 CouchDB 实例复制到另一个实例时也会调用。因此,对于一个文档,可以多次调用该函数。

但是,CouchDB 现在支持文档更新处理程序,可以修改文档甚至从头开始构建文档。这些可用于将非 JSON 输入数据转换为可用文档。您可以在 CouchDB Wiki 中找到一些文档。

在构建自己的用户注册/登录系统之前,我建议您研究一下内置的 CouchDB 安全功能(如果您还没有 - 一些信息 此处)。它们可能对您来说还不够(例如,如果您需要电子邮件验证或类似的东西),但也许您可以在它们的基础上进行构建。

The validate_doc_update function cannot have any side effects and cannot change the document before storage. It only has the power to block an update or to let it through. This is important, because the function is not only called when a user requests an update, but also when changes are replicated from one CouchDB instance to another. So the function can be called multiple times for one document.

However, CouchDB now supports Document Update Handlers that can modify a document or even build it from scratch. These can be used to convert non-JSON input data into usable documents. You can find some documentation in the CouchDB Wiki.

Before you build your own user registration/login system, I'd suggest you look into the built-in CouchDB security features (if you haven't - some information here). They might not be enough for you (e.g. if you need email validation or something similar), but maybe you can build on them.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文