小编bal*_*afi的帖子

使用属性名称的变量创建对象

是否可以在对象文字属性中使用变量名来创建对象?

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)

javascript properties object-literal

78
推荐指数
4
解决办法
8万
查看次数

如何将 async-await 与 MongoClient 一起使用

当我运行它时(使用带有 --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 node.js async-await

7
推荐指数
3
解决办法
1万
查看次数

使用节点的本机mongoDB驱动程序流查询结果

我在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)

javascript mongodb node.js

2
推荐指数
1
解决办法
2414
查看次数