gpt4 book ai didi

javascript - 使用 Node 在rehindb中插入错误

转载 作者:太空宇宙 更新时间:2023-11-04 00:58:39 25 4
gpt4 key购买 nike

我是 rethinkdb 的新手。当我尝试 https://github.com/rethinkdb/rethinkdb-example-nodejs/tree/master/todo-angular-express 中的示例代码时

function create(req, res, next) {
var todo = req.body;
todo.createdAt = r.now(); // Set the field `createdAt` to the current time

r.table('todos').insert(todo, {returnVals: true}).run(req._rdbConn, function(error, result) {
if (error) {
handleError(res, error)
}
else if (result.inserted !== 1) {
handleError(res, new Error("Document was not inserted."))
}
else {
res.send(JSON.stringify(result.new_val));
}
next();
});

}

我收到以下错误:

500 Internal Server Error

{"error":"return_vals renamed to return_changes in:\nr.table(\"todos\").insert({title: r.json(\"\"abcde\"\"), completed: r.json(\"false\"), createdAt: r.now()}, {returnVals: true})\n

然后我尝试了 http://rethinkdb.com/docs/examples/node-todo/ 中的示例代码

function create(req, res, next) {
var todo = req.body; // req.body was created by `bodyParser`
todo.createdAt = r.now(); // Set the field `createdAt` to the current time

r.table('todos').insert(todo, {returnChanges: true}).run(req._rdbConn, function(error, result) {
if (error) {
handleError(res, error)
}
else if (result.inserted !== 1) {
handleError(res, new Error("Document was not inserted."))
}
else {
res.send(JSON.stringify(result.changes[0].new_val));
}
next();
});

}

我收到以下错误:

500 Internal Server Error {"error":"Unrecognized optional argument returnChanges. in:\nr.table(\"todos\").insert({title: r.json(\"\"abcde\"\"), completed: r.json(\"false\"), createdAt: r.now()}, {returnChanges: true})\n "}

看来rethinkdb已将returnVals更改为return_changes/returnChanges,以及insert()的参数。

当我使用 return_changes 时,问题已得到解决。

在最新版本中插入的正确方法是什么?rethinkdb 总是改变它的语法吗?

最佳答案

这确实是示例代码中的一个错误。我已经打开https://github.com/rethinkdb/rethinkdb-example-nodejs/issues/3这样我们就可以修复它。

无法识别 returnChanges 的第二个问题可能来自于使用旧的 RethinkDB Node 驱动程序。您是否尝试过更新驱动程序? http://rethinkdb.com/docs/install-drivers/javascript/

关于javascript - 使用 Node 在rehindb中插入错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28237225/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com