数据存储区 - 为其中一个属性字段设置主键

问题描述 投票:0回答:1

我在我的申请中使用gstore-node

const gstore = require('gstore-node')();

const { Schema } = gstore;

const userSchema = new Schema({
  name: { type: String },
  userId: String,
});

我想让userId我的key。所以我可以做的事情

const userId = '1234';
userSchema.get(userId)

我试过了:

  const entityKey = UserSchema.key(userId);
  const user = new UserSchema({ key: entityKey });

 UserSchema.get(userId); // Not Found

我怎样才能做到这一点?

google-cloud-datastore datastore
1个回答
1
投票

弄清楚了。实体创建中的第二个arg是关键所以const user = new UserSchema({name: 'Homer'}, userId);

这使得userId成为关键,因此我可以使用get函数查找用户

© www.soinside.com 2019 - 2024. All rights reserved.