electron (redis-om)TypeError:实体.toRedisJson不是函数

83qze16e  于 6个月前  发布在  Electron
关注(0)|答案(1)|浏览(52)

我尝试使用Redis为我的数据库,我想使用电子redis-om,我不知道他们是否兼容,但我得到了错误。
(node:7024)UnhandledPromiseRejectionWarning:TypeError:实体.toRedisJson不是函数
我不知道我该怎么弥补我刚刚搞砸了什么?
//main.js//第一节没有问题,我可以保存数据到Redis

const { Entity, Schema, Client, Repository } = require('redis-om')
const client = new Client()
client.open('redis://localhost:6379')

class Album extends Entity {}
const albumSchema = new Schema(Album, {
  artist: { type: 'string' },
  title: { type: 'text' },
  year: { type: 'number' },
  genres: { type: 'string[]' },
  outOfPublication: { type: 'boolean' }
})

const albumRepository = client.fetchRepository(albumSchema);
const album = albumRepository.createEntity()
album.artist = 'Mushroomhead'
album.title = 'The Righteous & The Butterfly'
album.year = 2014
album.genres = [ 'metal' ]
album.outOfPublication = true
album = albumRepository.save(album)

字符串
//当我添加下面的代码尝试更改记录并保存它时,我会得到错误消息。

album = albumRepository.fetch('01H2J234SXNXM34A3BZF60HW99');
album.title = ' test'
album = albumRepository.save(album)


我尝试通过Redis-OM将数据保存到Redis,然后更新现有数据。

tvokkenx

tvokkenx1#

对不起,我以前的回答,我认为这是一个版本问题,并没有测试,因为我没有安装0.3版本。
经过0.3版的一些测试后,我想我发现了这个问题:你必须等待save()fetch()函数。
所以与其

album.outOfPublication = true
album = albumRepository.save(album)
album = albumRepository.fetch('01H2J234SXNXM34A3BZF60HW99');
album.title = ' test'
album = albumRepository.save(album)

字符串
你应该

album.outOfPublication = true;
album = await albumRepository.save(album); // await here, this will return an entityId
album = await albumRepository.fetch(album); // await here, this will return an Entity object, without await you would get a pending Promise
album.title = "test";
album = albumRepository.save(album);


fetch()save()根据redis-om文档进行了重新配置。

相关问题