亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何移動代碼以從我的文件中設置 ut DB 和集合并只需要它?

如何移動代碼以從我的文件中設置 ut DB 和集合并只需要它?

慕絲7291255 2023-02-24 15:31:17
所以,假設我有這段完美運行的代碼。const {  Database} = require("arangojs");var db = new Database({  url: "http://localhost:8529"});const database_name = "cool_database";db.useBasicAuth("username", "password123");db.listDatabases()  .then(names => {    if (names.indexOf(database_name) > -1) {      db.useDatabase(database_name);      db.get();    } else {      db.createDatabase(database_name)        .then(() => {          db.useDatabase(database_name);          db.collection("my-collection").create();        });    }  });const collection = db.collection("my-collection");const getJobFromQueue = () => {  return db.query({      query: "FOR el IN @@collection FILTER DATE_TIMESTAMP(el.email.sendAfter) < DATE_NOW() AND el.status != 'processed' AND el.status != 'failed' SORT el.email.sendAfter LIMIT 1 RETURN el",      bindVars: {        "@collection": "my-collection"      }    })    .then(cursor => cursor.all());}但是我想將最上面的代碼移到另一個文件中,只需要 db 和 collection,我該怎么做呢?一直在努力讓它工作太久了。const {  db,  collection} = require("./db");const getJobFromQueue = () => {  return db.query({      query: "FOR el IN @@collection FILTER DATE_TIMESTAMP(el.email.sendAfter) < DATE_NOW() AND el.status != 'processed' AND el.status != 'failed' SORT el.email.sendAfter LIMIT 1 RETURN el",      bindVars: {        "@collection": "my-collection"      }    })    .then(cursor => cursor.all());}
查看完整描述

1 回答

?
MMMHUHU

TA貢獻1834條經驗 獲得超8個贊

完全按照你的建議去做。將代碼的上半部分移動到db.js并公開db和collection使用exports:


數據庫.js:


const {

    Database

} = require("arangojs");

  

var db = new Database({

    url: "http://localhost:8529"

});


const database_name = "cool_database";  

db.useBasicAuth("username", "password123");

db.listDatabases()

  .then(names => {

    if (names.indexOf(database_name) > -1) {

      db.useDatabase(database_name);

      db.get();

    } else {

      db.createDatabase(database_name)

        .then(() => {

          db.useDatabase(database_name);

          db.collection("my-collection").create();

        });

    }

  });


exports.collection = db.collection("my-collection");

exports.db = db;

索引.js:


const {

  db,

  collection

} = require("./db");


const getJobFromQueue = () => {

  return db.query({

      query: "FOR el IN @@collection FILTER DATE_TIMESTAMP(el.email.sendAfter) < DATE_NOW() AND el.status != 'processed' AND el.status != 'failed' SORT el.email.sendAfter LIMIT 1 RETURN el",

      bindVars: {

        "@collection": "my-collection"

      }

    })

    .then(cursor => cursor.all());

}

警告:


請記住,您的代碼中存在潛在的競爭條件。在它們被初始化之前,您可能最終會使用dband 。collection


查看完整回答
反對 回復 2023-02-24
  • 1 回答
  • 0 關注
  • 162 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號