【发布时间】:2021-07-06 14:56:32
【问题描述】:
作为我的 GraphQL 解析器之一,我有这个函数,它可以将艺术家 ID 添加到用户的 Liked Artists 对象 ID 数组中。代码如下:
async likeArtist(_parent, args, _context, _info) {
await User.findOneAndUpdate(
{ _id: args.userID },
{ $push: { likedArtists: new ObjectId(args.artistID as string) } },
{
new: true,
runValidators: true,
}
);
return true;
},
当我在实际网站上试用时,它似乎工作正常。奇怪的是,$push 所在的行抛出了一个错误,特别是在“likedArtists”处:
... gave the following error.
Type 'ObjectId' is not assignable to type 'never'
这是用户的架构
import mongoose from "mongoose";
const UserSchema = new mongoose.Schema({
image: String,
email: String,
posts: Array,
likedPosts: Array,
likedArtists: Array,
balance: String,
notifications: Array,
tutorial: {
type: Boolean,
default: true,
},
name: String,
age: String,
country: String,
birthday: String,
phone: String,
newUser: Boolean,
notifRead: Boolean,
artLevel: String,
artStyles: Array,
artKinds: Array,
userBio: String,
// More to come
});
export default mongoose.models.User || mongoose.model("User", UserSchema);
如何消除打字稿错误?当我尝试npm run build 时,它会妨碍我。
【问题讨论】:
标签: node.js arrays mongodb typescript mongoose