如何等到mongodb获取值然后通过函数返回它

时间:2018-04-29 16:41:38

标签: node.js mongodb asynchronous

我想要一个函数来返回' user'中的内容。采集。但由于异步自然函数返回undefined。我怎么能等到mongodb为变量赋值?

    protected async override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);

        SetContentView(Resource.Layout.layout);
        string price = "";
        var db = new SQLiteConnection(Class1.dpPath);
        var table = db.Table<Table>();
        foreach(var item in table)
        {
            if(item.value != "0" && item.quantity != "0")
            {
                price = await GetpriceAsync(item.name); // here is the line of the await keyword
                Class1.tableItems.Add(new TableItem(item.quantity, price, "1000"));
            }
        }
    }

1 个答案:

答案 0 :(得分:0)

function b(m) {
    console.log(m);
}

function a() {
    MongoClient.connect(URL).then(db => {
        db.db('mydb').collection('user')
            .find({}).toArray().then(result => {

                b(result);

            }).catch(log);

    }).catch(log);

}