Submitting form to couchDB through update handler not working

99封情书 提交于 2019-12-24 00:28:59

问题


I can not post to CouchDB through an update handler and I do not know what I am doing wrong. Below follows the long description.

I created an app using erica, with details taken primarily from the wiki. It worked fine until I decided to go for POSTing, but server-side, through an update handler according to Apache CouchDB wiki Working_with_Forms

I created a new 'webapp' with erica, constructed an index (cut-n-paste from the wiki, with small alterations):

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Minimal Form</title>
</head>

<body>
<div id="contact-form">
  <form id="contact" method="post" action="/mydatabase/_design/mydesigndoc/_update/postForm" enctype="multipart/form-data">
    <fieldset>
        <label for="name">name</label>
        <input type="text" name="name" placeholder="Full Name" title="Enter your name" class="required">

        <label for="phone">phone</label>
        <input type="tel" name="phone" placeholder="+1 (555) 555-5555" required="" pattern="\+?[0-9 )(-]+">

        <label for="email">e-mail</label>
        <input type="email" name="email" placeholder="you@example.org" title="e-mail address" class="required email">

        <label for="blog">blog</label>
        <input type="url" name="url" placeholder="http://">

        <label for="message">message</label>
        <textarea name="message"></textarea>

        <input type="submit" name="submit" class="button" id="submit" value="submit">
    </fieldset>
  </form>
</div>

I altered the form-attribute action= to: htttp://localhost:5984/DBNAME/_design/DESIGNDOCID/_update/UPDATENAME and added enctype="multipart/form-data".

Then a _design document was constructed, according to the wiki, like this:

{
updates: {
    postForm: function(previous, request) {

        /* during development and testing you can write data to couch.log
    log({"previous": previous})
    log({"request": request})
    */
    var doc = {}
    if (!previous) {
            // there's no existing document _id as we are not using PUT
            // let's use the email address as the _id instead
            if (request.form && request.form.email) {
                // Extract the JSON-parsed form from the request
                // and add in the user's email as docid
                doc = request.form
                doc._id = request.form.email
            }
        }
        return [doc, toJSON({
            "request": request,
            "previous": previous,
            "doc": doc
        })]
    }
}
}

This was placed in the "ddoc"-folder, pushed the app with erica, opened the webpage according to the link, found the form but when it was submitted this is what answer I got:

{"error":"unnamed_error","reason":"(new TypeError(\"point is undefined\", \"/usr/share/couchdb/server/main.js\", 1475))"}

I have fiddled around with the action="..." attribute, even put absolute adress like this:

http://localhost:5984/mydatabase...

I have replaced the toJSON() with JSON.stringify().

I have restarted the process and done the project it all over again. To no avail.

I have the distinct feeling that I have gone "blind", and that the solution is probably just in front of my eyes but I cannot see it. Seems like there is no problem with the POST-http-request, cause the server has complained before when I have experimented with AJAX (forgot "content-type"), but this time it seems to be internal server problems. And I do not have a clue. Really.

All in all, the question is: Can somebody help me? Please.


回答1:


I will answer my own question and at the same time ask forgiveness from those who wasted their time with it.

What I did is that I read through kanso and understood how the concept of scope applies to the situation. It is a matter of using exports on the update handler so it can be reached through <form action="/database/_design/designDocument/_update/updateFunction.

Why did I not read through Kan.so earlier? Well, I had my mind set on keeping it simple - erica being the successor of couchapp I decided that it would be a sound move to stay on course with the basics. Though I must say that documentation is scarce so the magic of couchapp-building was demystified by reading through Kan.so and on top of it I was introduced to several other nifty concepts and techniques. I bend my neck in gratitude.

And I hope that all those who have spent their time reading through my long and, as it turned out, unnecessary question will oversee with my ignorance.

(Now I only wonder if there is some kind of admin/moderator who can dispose of my writings to avoid future timel oss)




回答2:


I also ran into this error and what causes it, as hinted at by @Pea-pod, is not defining properly your exports in the couchapp's design documents. In our case it was as list function that couldn't be called and instead displayed a 500 error with Type error and point is undefined in the couchdb log.

We use kanso and in the app.js we hadn't required the list file. We had:

module.exports = {
    rewrites: require('./rewrites'),
    views: require('./views'),
    shows: require('./shows')
};

Changing it to the following solved the problem:

module.exports = {
    rewrites: require('./rewrites'),
    views: require('./views'),
    shows: require('./shows'),
    lists: require('./lists')
};

Can I suggest to a moderator to change the title of this question to include point is undefined which is the error that shows up in the CouchDB log when this type of error is made, in order to help others find it more easily?



来源:https://stackoverflow.com/questions/17908321/submitting-form-to-couchdb-through-update-handler-not-working

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!