从Node.js中的Cloudant数据库中删除文档 [英] Deleting a document from Cloudant Database in nodejs
问题描述
这可能是一个基本问题,但我浏览了github Cloudant库和Cloudant文档,并提到了从数据库中删除特定文档的情况,但从未进行彻底解释。非常令人沮丧我最想删除文档的方法是使用http请求,而不是Cloudant库提供的功能,即使我正在通过_rev传递文档,我也不断收到文档更新冲突。谁能解释使用nodejs从Cloudant数据库中删除文档的示例,以帮助您解决该问题。谢谢。
This may be a basic question but I've looked through the github Cloudant library and the Cloudant documentation and deleting a specific document from the database is mentioned but never thoroughly explained. It's very frustrating. The closest I've gotten to deleting a document is using an http request rather then the functions Cloudant library offers and I continuously get a "Document Update Conflict" even though I'm passing through the _rev of the document. Can anybody explain deleting a document from a Cloudant database using nodejs with an example to help sort this out. Thanks.
推荐答案
您可以使用
但是由于您正在发送_rev并收到文档更新冲突错误,这使我怀疑您是否拥有最新的_rev。如果本地_rev与远程_rev不匹配,则通常会发生文档更新冲突。因此,我建议将 destroy
函数包装在 get
函数中。因此,对@JakePeyser的示例进行的更新将是:
You can use the destroy
method of nano like @JakePeyser has said, instead of using http APIs since you are using nodejs.
But since you are sending _rev and getting a "Document Update Conflict" error, it leads me to doubt if you have the latest _rev with you. "Document Update Conflict" happens mostly if the local _rev doesn't match the remote _rev. I would therefore suggest wrapping your destroy
function in a get
function. So an update to @JakePeyser's example would be:
var nano = require("nano")("cloudantURL"),
db = nano.db.use("yourDB");
db.get(docUniqueId, function(err, body) {
if (!err) {
var latestRev = body._rev;
db.destroy(docUniqueId, latestRev, function(err, body, header) {
if (!err) {
console.log("Successfully deleted doc", docUniqueId);
}
});
}
})
这篇关于从Node.js中的Cloudant数据库中删除文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!