收集后POST响应错误.插入NodeJS Mongo模块
POST response error after collection.insert with NodeJS Mongo module
我在尝试POST 时遇到此错误
> process.nextTick(function() { throw err; });
> ^
>
> TypeError: first argument must be a string or Buffer
> at ServerResponse.OutgoingMessage.end (_http_outgoing.js:524:11)
错误表明mongodb模块中的utils和cursor都有问题,但它们是什么?
在GET上一切都很好,但在POST上停止了(邮递员和传递文本{"name":"Computer","price":2500})-我无法跟踪哪个模块或实例正在停止代码。
这是我与数据库的连接:
// Our primary interface for the MongoDB instance
var MongoClient = require('mongodb').MongoClient;
// Used in order verify correct return values
var assert = require('assert');
var connect = function (databaseName, callBack) {
var url = 'mongodb://localhost:27017/' + databaseName;
MongoClient.connect(url,
function (error, database) {
assert.equal(null, error);
console.log("Succesfully connected to MongoDB instance!");
callBack(database);
});
};
exports.find = function (databaseName, collectionName, query, callback) {
connect(databaseName, function (database) {
var collection = database.collection(collectionName);
collection.find(query).toArray(
// Callback method
function (err, documents) {
// Make sure nothing went wrong
assert.equal(err, null);
// Print all the documents which we found, if any
console.log("MongoDB returned the following documents:");
console.dir(documents)
callback(err, documents);
// Close the database connection to free resources
database.close();
})
})
};
exports.insert = function (databaseName, collectionName, object, callback) {
connect(databaseName, function (database) {
var collection = database.collection(collectionName);
collection.insert(document, {w: 1}, function (err, documents) {
console.log("Added a new document");
console.log(documents[0]);
callback(err, documents[0]);
});
})
};
exports.remove = function (databaseName, collectionName, object, callback) {
connect(databaseName, function (database) {
var collection = database.collection(collectionName);
collection.remove(object, function (err, result) {
callback(err, result);
database.close();
});
})
};
这个问题实际上非常简单,所以我很惊讶你没有得到更好的错误消息。
在您的代码中:
collection.insert(document, {w: 1}, function (err, documents) {
console.log("Added a new document");
console.log(documents[0]); // I expect this to log undefined
callback(err, documents[0]);
});
传递到collection.insert
回调的第二个参数实际上是一个结果对象,而不是插入的文档。因此,documents[0]
最终成为undefined
,因为它不是一个文档数组。因此,当您尝试发送undefined
作为响应时,它是失败的。
如果您打算传递新创建的文档,则必须使用result
对象来获取_id并将其附加到插入的文档。
顺便说一句,我会考虑保持与数据库的连接打开,而不是每次您想与Mongo交谈时都创建一个新的连接。
相关文章:
- nodejs-expressjs上传图像并显示它们
- 将样式表插入iframe
- Ckeditor-plugin:插入虚假元素add不情愿<p>标签前后
- 如何动态插入jquery代码
- 如何在javasciipt中添加单引号到字符串,该字符串将在nodeJS中作为mysql插入命令执行
- 使用Nodejs在Mysql中插入多个值,并用响应通知用户
- 收集后POST响应错误.插入NodeJS Mongo模块
- 如何在使用nodejs和mysql进行抓取时避免插入数据库时的冗余
- 自动插入分号:NodeJS与Browser上的可选强制转换行为
- mongodb-nodejs多插入问题
- 如何使用google-api-nodejs-client在时间轴中插入附件
- 插入一个事件与nodejs谷歌日历API返回400:"缺失的结束时间"
- 插入不工作在Nodejs和Mysql
- Nodejs, MongoDB -确定集合中的文档是否被更新或插入
- NodeJS -如何向MySQL插入多行
- 使用nodejs在mongodb中插入最多数量的记录
- NodeJS+Express+Mongoose(MongoDB)数据库插入错误
- 在 NodeJS 中获取 Mongo 数据库中插入文档的_id
- 在行号 nodejs 处插入字符串
- 插入行时,Nodejs mysql模块错误