执行 npx babel-node src/config/test.js 报错

来源:4-2 Redis实战应用配置—get&set方法

unbreakable_全栈

2021-12-04 19:48:27

--------------------------

https://img.mukewang.com/climg/61ab551009852baf19001006.jpg

RedisConfig.js

// import redis from 'redis'
// import { promisifyAll } from 'bluebird'
// import config from './index'
// // import log4js from '@/config/Log4j'

// // const logger = log4js.getLogger('out')

// const options = {
//   host: config.REDIS.host,
//   port: config.REDIS.port,
//   password: config.REDIS.password,
//   no_ready_check: true,
//   detect_buffers: true,
//   retry_strategy: function (options) {
//     if (options.error && options.error.code === 'ECONNREFUSED') {
//       // End reconnecting on a specific error and flush all commands with
//       // a individual error
//       return new Error('The server refused the connection')
//     }
//     if (options.total_retry_time > 1000 * 60 * 60) {
//       // End reconnecting after a specific timeout and flush all commands
//       // with a individual error
//       return new Error('Retry time exhausted')
//     }
//     if (options.attempt > 10) {
//       // End reconnecting with built in error
//       console.log('already tried 10 times!')
//       return undefined
//     }
//     // reconnect after
//     return Math.min(options.attempt * 100, 3000)
//   }
// }

// // const client = redis. createClient(options)
// let client = promisifyAll(redis.createClient(options))

// client.on('error', (err) => {
//   console.log('Redis Client Error:' + err)
// })

// const setValue = (key, value, time) => {
//   if (!client.connected) {
//     client = promisifyAll(redis.createClient(options))
//   }
//   if (typeof value === 'undefined' || value == null || value === '') {
//     return
//   }
//   if (typeof value === 'string') {
//     if (typeof time !== 'undefined') {
//       client.set(key, value, 'EX', time, (err, result) => {
//         console.log('client.set -> err', err)
//       })
//       // client.set(key, value);
//       // client.expire(key, time);
//     } else {
//       client.set(key, value)
//     }
//   } else if (typeof value === 'object') {
//     // { key1: value1, key2: value2}
//     // Object.keys(value) => [key1, key2]
//     Object.keys(value).forEach((item) => {

//     })
//   }
//   // client.quit()
// }

// // const {promisify} = require('util');
// // const getAsync = promisify(client.get).bind(client);

// const getValue = (key) => {
//   if (!client.connected) {
//     client = promisifyAll(redis.createClient(options))
//   }
//   return client.getAsync(key)
// }

// const getHValue = (key) => {
//   // v8 Promisify method use util, must node > 8
//   // return promisify(client.hgetall).bind(client)(key)

//   // bluebird async
//   return client.hgetallAsync(key)
// }

// const delValue = (key) => {
//   client.del(key, (err, res) => {
//     if (res === 1) {
//       console.log('delete successfully')
//     } else {
//       console.log('delete redis key error:' + err)
//     }
//   })
// }
// export { client, setValue, getValue, getHValue, delValue }

/**
 * 
 */
import redis from 'redis'

const options = {
  host: '192.168.43.254', //虚拟机IP地址 可能是 我自己不确定
  port: 15001,
  password: '123456',
  detect_buffers: true,
  retry_strategy: function (options) {
    if (options.error && options.error.code === 'ECONNREFUSED') {
      // End reconnecting on a specific error and flush all commands with
      // a individual error
      return new Error('The server refused the connection')
    }
    if (options.total_retry_time > 1000 * 60 * 60) {
      // End reconnecting after a specific timeout and flush all commands
      // with a individual error
      return new Error('Retry time exhausted')
    }
    if (options.attempt > 10) {
      // End reconnecting with built in error
      return undefined
    }
    // reconnect after
    return Math.min(options.attempt * 100, 3000)
  }
}

const client = redis.createClient(options)

const setValue = (key, value) => {
  return client.set(key, value)
}


const {promisify} = require('util');
const getAsync = promisify(client.get).bind(client);

const getValue = (key) => {
  return getAsync(key)
}

export {
  client,
  setValue,
  getValue
}
----------------------
test.js
import {getValue, setValue} from './RedisConfig'

setValue('imooc', 'imooc message from redis client')
getValue('imooc').then((res) => {
   console.log('getValue => ', res)
})


写回答

1回答

Brian

2021-12-09

从截图上看看不出来什么错误。


可以检查你自己的redis的版本:



https://git.imooc.com/class-130/api-start/src/master/package.json#L46

0
hnbreakable_全栈
hp>https://img.mukewang.com/climg/61b894d9096eb51a19041006.jpg老师 我这里成功啦 我把tobo3t 还有 redis 都打通之后,再过来看这里的问题时,自己就好啦 ,感谢老师的帮助。

h021-12-14
共1条回复

大前端2022版

1个业务场景打通全端、全栈、全流程,实现全局思维与综合技术能力全面突破;对标阿里P6,8个月成为前端高级人才

2840 学习 · 691 问题

查看课程