是否可以在对象文字属性中使用变量名来创建对象?
例
function createJSON (propertyName){
return { propertyName : "Value"};
}
var myObject = createJSON("myProperty");
console.log(myObject.propertyName); // Prints "value"
console.log(myObject.myProperty); // This property does not exist
Run Code Online (Sandbox Code Playgroud) 当我运行它时(使用带有 --harmony 的节点 v7.5.0):
var MongoClient = require('mongodb').MongoClient,
var url = "mongodb://localhost:27017/myDB";
var test = await MongoClient.connect(url);
module.exports = test;
Run Code Online (Sandbox Code Playgroud)
我收到此错误:
var test = await MongoClient.connect(url);
^^^^^^^^^^^
SyntaxError: Unexpected identifier
Run Code Online (Sandbox Code Playgroud)
MongoClient.connect(url) 确实返回了一个承诺
我最终想要实现的是创建一个节点模块,该模块将连接到 mondoDB 并且可以像以下示例一样使用:
var db = require('../utils/db'); //<-- this is what I want to create above
col = db.collection('myCollection');
module.exports.create = async fuction(data) {
return await col.insertOne(data);
}
Run Code Online (Sandbox Code Playgroud)
有什么建议?
我在mongoDB集合中有100,000条记录,并尝试使用本机驱动程序在node.js应用程序中检索它们.
我在MongoDB doc中为CursorStream执行示例,但得到错误:
RangeError: Maximum call stack size exceeded
Run Code Online (Sandbox Code Playgroud)
在此错误之前,我得到了很多:
(node) warning: Recursive process.nextTick detected. This will break in the next version of node. Please use setImmediate for recursive deferral.
Run Code Online (Sandbox Code Playgroud)
这是我的代码:
var query = {...};
var fields = {...};
var options = {
// "limit": 10
//"skip": 10,
//"sort": title
}
var stream = myCollection.find(query, fields, options).stream();
// stream.pause();
var results = [];
stream.on('data', function (item){
results.push(item);
stream.pause();
// Restart the stream after 1 miliscecond
setTimeout(function() {
stream.resume(); …Run Code Online (Sandbox Code Playgroud)