我正在关注一个教程,我创建了一个 cache.js 文件,该文件将 mongoose 查询和 JSON.stringifying 其转换为该查询返回的值的键。我们的目标是缓存,然后追加.cache()内app.js其中mongoose.find()是
当前,如果缓存为空,我让它从数据库中执行 GET,然后将其存储在缓存中。我有一个
console.log("CACHE VALUE #2");
console.log(cacheValue1);
Run Code Online (Sandbox Code Playgroud)
这可确保数据得到存储并成功输出数据。这条线有效。但有了这条线,
console.log("CACHE VALUE #1");
console.log(cacheValue);
Run Code Online (Sandbox Code Playgroud)
在cacheValue为空。
这是为什么?
它存储在底部的值和键永远不会改变所以我不明白为什么它不会返回数据而不是空值。
SoCache Value #1始终为空并Cache Value #2具有正确的数据。
控制台输出:
GRABBING FROM DB
CLIENT CONNECTION STATUS: true
Setting CACHE to True
ABOUT TO RUN A QUERY
{"$and":[{"auctionType":{"$eq":"publicAuction"}},{"auctionEndDateTime":{"$gte":1582903244869}},{"blacklistGroup":{"$ne":"5e52cca7180a7605ac94648f"}},{"startTime":{"$lte":1582903244869}}],"collection":"listings"}
CACHE VALUE #1
null
CACHE VALUE #2
(THIS IS WHERE ALL MY DATA SHOWS UP)
Run Code Online (Sandbox Code Playgroud)
const mongoose = require('mongoose');
const redis = require('redis');
const util = …Run Code Online (Sandbox Code Playgroud) 我被困在这里了一段时间我不明白这个问题。请有人在这个主题上启发我。这是代码。
const puppeteer = require('puppeteer');
(async() => {
let infourl = 'https://www.imdb.com/title/tt0111161/?ref_=fn_al_tt_3';
let browser = await puppeteer.launch();
let page = await browser.newPage();
await page.goto(infourl, { waitUntil:'networkidle2' });
let data = await page.evaluate( () =>{
let stats = document.querySelector('div[class="title_wrapper"]').innerText;
return {stats};
});
console.log(data);
debugger;
await browser.close();
})();
Run Code Online (Sandbox Code Playgroud)
这是日志::
internal/util.js:209
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'original', 'function');
^
TypeError [ERR_INVALID_ARG_TYPE]: The "original" argument must be of type function
at promisify (internal/util.js:209:11)
at Object.<anonymous> (/home/hadi/Desktop/datachori/node_modules/extract-zip/index.js:11:18)
at Module._compile (module.js:652:30)
at Object.Module._extensions..js (module.js:663:10)
at Module.load (module.js:565:32)
at …Run Code Online (Sandbox Code Playgroud) 我需要开玩笑地为一个函数编写一个测试。我有如下功能:
async function fun(conn, input){
const connection = conn.getConnection();
....
// some output gets generated from input
// const processed_input = input???;
....
const x = util.promisify(connection.query).bind(connection);
return /* await */ x(processed_input);
}
Run Code Online (Sandbox Code Playgroud)
我希望将其值processed_input传递给 function x。
我认为类似的东西.toHaveBeenCalledWith应该有效,但我不确定它如何适用于承诺的绑定函数。
我还尝试模拟查询,执行类似于调用conn.getConnection = { query: jest.fn() }之前的fun操作,但我不确定如何继续进行expect。
expect更新:到目前为止,我当前的解决方案是在查询函数中包含 jest语句。
conn.getConnection = {
query: function(processed_input){ //expect processed_input to be; }
}`
Run Code Online (Sandbox Code Playgroud)
希望有更好的方法。
node.js ×3
asynchronous ×1
jestjs ×1
mocking ×1
mongoose ×1
puppeteer ×1
redis ×1
unit-testing ×1