小编Vae*_*lin的帖子

无法在 mongoose 中使用泛型模型:“x”类型的参数不可分配给 MongooseFilterQuery 类型的参数

我正在尝试将通用 Mongoose 模型作为参数传递给函数。

import mongoose, { Document, Model, Schema } from 'mongoose';

interface User {
    name: string;
    age: number;
    favouriteAnimal: string;
}

const UserSchema = new Schema({
    name: String,
    age: Number,
    favouriteAnimal: String,
});

const UserModel = mongoose.model<User & Document>('User', UserSchema);

function doSomething() {
    return UserModel.findOne({ user: 'john' }); // This is fine.
}

async function doSomethingGeneric<T extends User & Document>(model: Model<T>, key: keyof T) {
    const user = await model.findOne({ user: 'john' }); // Compiler errors on this line. …
Run Code Online (Sandbox Code Playgroud)

mongoose mongodb node.js typescript

9
推荐指数
1
解决办法
3984
查看次数

为什么从 float64 到 int 的转换会导致值增加 1?

630948893921274879float64 类型转换为 int 时,我希望结果为630948893921274879. 但是,实际结果是630948893921274880,比 1 大。

这是什么原因?

import (
    "fmt"
)

func main() {
    var p float64 = 630948893921274879
    fmt.Println(int(p))   // 630948893921274880
    fmt.Printf("%f\n", p) // 630948893921274880.000000
}

Run Code Online (Sandbox Code Playgroud)

https://play.golang.org/p/gbXKCkZ6_rF

go

-1
推荐指数
2
解决办法
98
查看次数

标签 统计

go ×1

mongodb ×1

mongoose ×1

node.js ×1

typescript ×1