[英]How to perform Update and Delete operations in a bucket using Couchbase and Nodejs sdk
I am moving from mongodb to Couchbase using Node.js. 我正在使用Node.js从mongodb迁移到Couchbase。 I want to perform the CRUD operations.
我想执行CRUD操作。 Insert(create) and Get are working fine, but when I want to perform Update and Delete getting some error messages (Here update purpose using 'upsert','replace' are used) like:
Insert(create)和Get可以正常工作,但是当我要执行Update和Delete时,会收到一些错误消息(此处使用'upsert','replace'进行更新)是这样的:
TypeError: Cannot read property 'replace' of undefined
TypeError:无法读取未定义的属性“替换”
Here is code: 这是代码:
db.js
db.js
// Instantiate Couchbase and Ottoman
var couchbase=require('couchbase');
var ottoman=require('ottoman');
// Build my cluster object and open a new cluster
var myCluster = new couchbase.Cluster('localhost:8091');
var myBucket = myCluster.openBucket('default');
ottoman.bucket=myBucket;
require('./model/user');
ottoman.ensureIndices(function(){});
user.js user.js
var db = require('./../db.js').myBucket;
var ottoman = require('ottoman');
var userMdl = ottoman.model('User', {
firstName: {type:'string'},
lastName: {type:'string'},
created: {type: 'Date', default:function(){return new Date()}},
email:'string',
phone: 'string'
},{
index: {
findByID: {
by: '_id'
},
}
})
module.exports = userMdl;
routes.js routes.js
var bodyParser = require('body-parser');
var db = require('../schema/db').myBucket;
var user=require('../schema/model/user');
var jsonParser = bodyParser.json();
var urlencodedParser = bodyParser.urlencoded({ extended: false });
module.exports = function (app) {
// Delete a record
app.post("/api/delete/:_id", function(req, res) {
console.log("_id:"+req.params._id)
if(!req.params._id) {
return res.status(400).send({"status": "error", "message": "A document id is required"});
}
db.delete({_id:req.params._id}, function(error, result) {
if(error) {
return res.status(400).send(error);
}
res.send(result);
});
});
app.post('/api/user/update/:id',function(req,res){
db.replace(req.params.id,{firstName:"Mahesh"},function(err,result){
if (err) {
res.status = 400;
res.send(err);
return;
}
else {
res.status = 202;
res.send(result);
}
})
})
}
I am stuck here from last two days. 最近两天我一直被困在这里。
You missed one argument although it can be optional. 您错过了一个参数,尽管它可以是可选的。
From Couchbase Node.js SDK document , it have 4 arguments, but you have only 3. 在Couchbase Node.js SDK文档中 ,它有4个参数,但只有3个。
db.replace(req.params.id,{firstName:"Mahesh"},function(err,result){
=> =>
db.replace(req.params.id,{firstName:"Mahesh"}, {}, function(err,result){
With 3rd argument of empty map may work properly, but notice that Couchbase uses optimistic locking , so you require "CAS" value for original document when you modify the original to get data integrity. 使用空映射的第3个参数可能会正常工作,但是请注意Couchbase使用开放式锁定 ,因此在修改原始文档以获取数据完整性时,需要原始文档的“ CAS”值。
the line in db.js var ottoman = require('ottoman');
db.js中的行
var ottoman = require('ottoman');
it's a constructor itself. 它本身就是一个构造函数。 Then you have two instances, and the error comes in user.js when you try to define a model, because node-ottoman needs a reference to the bucket.
然后,您有两个实例,当您尝试定义模型时,错误出现在user.js中,因为node-ottoman需要对存储桶的引用。
You should assign the bucket in the user.js or reuse the ottoman object that you left in the db.js 您应该在user.js中分配存储桶,或重新使用在db.js中留下的奥斯曼对象
model.js model.js
// Instantiate Couchbase and Ottoman
var couchbase = require('couchbase');
var ottoman = require('ottoman');
// Build my cluster object and open a new cluster
var myCluster = new couchbase.Cluster('localhost:8091');
var myBucket = myCluster.openBucket('default');
ottoman.bucket = myBucket;
var userMdl = ottoman.model('User', {
firstName: {type:'string'},
lastName: {type:'string'},
created: {type: 'Date', default:function(){return new Date()}},
email:'string',
phone: 'string'
},{
index: {
findByID: {
by: '_id'
},
}
}) ;
// this line needs to be after you define the model
ottoman.ensureIndices(function(){});
module.exports = userMdl;
model.exports = mybucket;
You can update Couchbase document using 2 ways 1st by upsert
method and second by N1qlQuery
您可以使用两种方法更新Couchbase文档,第一种是
upsert
方法,第二种是N1qlQuery
bucket.upsert('user', {'name': 'Jay'}, {'expiry': 1}, function(err){
bucket.get('user', function(err, result) {
console.log('Have item: %j', result.value);
})
});
let query = N1qlQuery.fromString("UPDATE `"+BUCKETNAME+"` SET name='"+data.name+"' where _id ='"+id+"'");
bucket.query(query,(error,result)=>{
if(error){
console.log(error);
}
console.log(result);
});
You can delete Couchbase document using 2 ways 1st is removed
method and second by N1qlQuery
您可以使用2种方式删除Couchbase文档,第一种是通过
N1qlQuery
removed
方法,第二种是通过N1qlQuery
bucket.remove(id, function(error, result) {
console.log("Deleted");
});
let query = N1qlQuery.fromString("DELETE FROM `"+BUCKETNAME+"` WHERE _id = '"+id+"'");
bucket.query(query,(error,result)=>{
if(error){
console.log(error);
}
console.log(result);
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.