【问题标题】:Unable to make PUT request to Cloudant-bluemix database using Nodejs-express无法使用 Nodejs-express 向 Cloudant-bluemix 数据库发出 PUT 请求
【发布时间】:2015-11-13 15:51:21
【问题描述】:

我正在尝试实施密码重置。所以我正在获取用户的电话号码,使用电话号码从数据库中获取文档以查找它,并且我正在获取新密码并尝试使用我的 Cloudant 数据库中的 PUT 请求更新相应的文档。

app.post('/pass_rst', function(req,response){
var log='';
//log is just for me to see what's happening
var phone= req.body.phone;
log+=phone+'\n';

db.find({selector:{'phone':phone}}, function(err, result){
    if(err){
        throw err;
    }
    if(result.docs.length==0){
        response.send('User doesnt exist');
    }else{
        var existing_data=result.docs[0];
        log+=JSON.stringify(existing_data)+'\n';
        var upd_pswd= req.body.new_password;
        log+=upd_pswd+'\n';
        var new_data=existing_data;
        new_data.password=upd_pswd;
        log+=JSON.stringify(new_data)+'\n';

        var id= result.docs[0]._id;
        log+=id+'\n';

        //make PUT request to db
        var options={
            host:dbCredentials.host,
            port:dbCredentials.port,
            path:'/'+dbCredentials.dbName+'/'+id,
            //url: dbCredentials.url+'/'+dbCredentials.dbName+'/'+id,
            method:'PUT',
            json:new_data,
            headers:{
                'Content-Type':'application/json',
                'accept':'*/*'
            }
        };
        log+=JSON.stringify(options)+'\n';

        var httpreq= http.request(options);
        //log+=JSON.stringify(httpreq);

        httpreq.on('error', function(e){
            response.send('Error'+e.message);
        });

        response.send(log+'\n\n\nUpdated');
    }
  });
});

dbCredentials 上面定义如下:

dbCredentials.host = vcapServices.cloudantNoSQLDB[0].credentials.host;
        dbCredentials.port = vcapServices.cloudantNoSQLDB[0].credentials.port;
        dbCredentials.user = vcapServices.cloudantNoSQLDB[0].credentials.username;
        dbCredentials.password = vcapServices.cloudantNoSQLDB[0].credentials.password;
        dbCredentials.url = vcapServices.cloudantNoSQLDB[0].credentials.url;

我尝试过修改它,但在最好的情况下,我没有收到错误,我看到“已更新”,但数据库中实际上没有任何反应。有时我会收到一条错误消息:502 Bad Gateway: Registered endpoint failed to handle the request。

如果您发现出了什么问题,请告诉我。谢谢。

这是关于如何在 cloudant 中更新文档的文档

更新

更新文档

PUT /$DATABASE/$DOCUMENT_ID HTTP/1.1 { "_id": "apple", "_rev": "1-2902191555", "item": "Malus domestica", "prices": { “生鲜市场”:1.59, “最高价格”:5.99, “苹果快递”:0.79, “Gentlefop 的 Shackmart”:0.49 } }

要更新(或创建)文档,请使用更新后的内容发出 PUT 请求 JSON 内容和最新的 _rev 值(不需要创建新的 文件)到 https://$USERNAME.cloudant.com/$DATABASE/$DOCUMENT_ID。

如果您未能提供最新的 _rev,Cloudant 会以 409 响应 错误。此错误可防止您覆盖由其他人更改的数据 过程。如果无法满足写入仲裁,则返回 202 响应 返回。

示例响应:{ "ok":true, "id":"apple",
"rev":"2-9176459034" }

响应包含文档的 ID 和新修订版或 更新失败时的错误消息。

【问题讨论】:

  • 您是否能够连接到数据库并执行任何其他 CRUD 操作?
  • 是的,我正在使用 cloudant/nodejs 库 github.com/cloudant/nodejs-cloudant。我已经能够使用 db.insert 和 db.find 函数进行创建和读取操作,但我不确定是否有 db.update 函数,所以我试图直接向 cloudant db 发出 PUT 请求。到目前为止还没有奏效。

标签: node.js http put ibm-cloud cloudant


【解决方案1】:

我正在使用 bluemix -nodejs 和 cloudant。对我来说进行更新的最佳方法是使用 nano 包进行来自 node.js 的 db 交互。

您可以参考这里的帖子: 总结是 - 通过使用 nano api,您可以轻松更新记录。在使用 nano 时,您需要确保使用 - _id 和正确的 _rev 编号。这又在下面使用 PUT 方法。

Updating and Deleting documents using NodeJS on Cloudant DB

当您包含 nano 时,请确保更新 package.json 以添加 nano 依赖项。如果您对更新/删除还有其他问题,请告诉我

【讨论】:

  • 虽然此链接可能会回答问题,但最好在此处包含答案的基本部分并提供链接以供参考。如果链接页面发生更改,仅链接的答案可能会失效。
  • 好的..添加了几行,希望对您有帮助,Tx
【解决方案2】:

使用 cloudant node.js 模块时,没有单独的更新功能。 您还需要使用 db.insert 函数来使用正确的文档修订版进行更新,因此您需要在插入之前阅读最新的修订版。

https://github.com/apache/couchdb-nano#document-functions

"并且还用于更新现有文档,方法是在正在保存的文档中包含 _rev 标记:"

// read existing document from db
db.get(key, function(error, existing) {
    if (!error)
         // use revision of existing doc for new doc to update
         obj._rev = existing._rev;
    // call db insert
    db.insert(obj, key, cb);
});

【讨论】:

  • 感谢您的评论。我实际上尝试了这种方法,但它最终所做的只是创建一个新文档。然后我继续按照 cloudant 文档中的描述发送 PUT 请求。 docs.cloudant.com/document.html#update
猜你喜欢
  • 2019-06-21
  • 1970-01-01
  • 1970-01-01
  • 2018-05-04
  • 1970-01-01
  • 2020-10-27
  • 2021-01-06
  • 2021-06-08
  • 1970-01-01
相关资源
最近更新 更多