programing

Mongoose는 항상 빈 배열 노드를 반환합니다.제이에스

sourcejob 2023. 7. 8. 10:51
반응형

Mongoose는 항상 빈 배열 노드를 반환합니다.제이에스

사용해 보았습니다.find그리고.findOne둘 다 서류를 돌려주지 않고 있습니다 find다음 시간 동안 빈 배열을 반환합니다.findOne돌아오는 중null.err두 경우 모두에 있어서는null뿐만 아니라.

여기 제 연결고리가 있습니다.

function connectToDB(){
    mongoose.connect("mongodb://localhost/test"); //i have also tried 127.0.0.1
    db = mongoose.connection;
    db.on("error", console.error.bind(console, "connection error:"));
    db.once("open", function callback(){
        console.log("CONNECTED");
    });
};

내 스키마는 다음과 같습니다.

var fileSchema = mongoose.Schema({
    hash: String,
    type: String,
    extension: String,
    size: String,
    uploaded: {type:Date, default:(Date.now)},
    expires: {type:Date, default:(Date.now()+oneDay)}
});
var Model = mongoose.model("Model", fileSchema);

제 질문은 다음과 같습니다.

Model.find({},function(err, file) {
    console.log(err)
    console.log(file);  
});

나는 데이터베이스에 물건을 업로드하고 록몽고를 통해 볼 수 있지만 이후에는 가져올 수 없습니다.저는 MongoDB를 처음 사용하기 때문에 몇 가지 기본 사항을 놓치고 있는 것 같습니다.올바른 방향으로 밀고 나가는 것은 정말 좋을 것입니다!

호출:mongoose.model모델이 연결된 컬렉션의 이름을 설정합니다. 기본값은 복수화된 소문자 모델 이름입니다.그래서 당신의 코드로는, 그것은.'models'모델과 함께 사용하는 방법files컬렉션, 해당 줄을 다음으로 변경:

var Model = mongoose.model("Model", fileSchema, "files");

또는

var Model = mongoose.model("file", fileSchema);

단순하게 복수화 복잡성을 피하기 위해 다음을 사용합니다.

var Model = mongoose.model("Model", fileSchema, "pure name your db collection");

매우 혼란스러워요.[적어도 나에게는.]

같은 문제가 있었습니다.위의 해결책들은 저에게 효과가 없었습니다.내 앱은 쿼리를 찾지 못해도 오류를 반환하지 않습니다.빈 배열을 반환합니다.그래서 제 코드에 이것을 넣었습니다.

if(queryResult.length==0) return res.status(404).send("not found");

이 문제는 컬렉션 이름을 지정하지 않고 몽구스 모델을 만들고 있기 때문일 수 있습니다.

변경 시도:const Model = mongoose.model("Model", fileSchema);

수신인:const Model = mongoose.model("Model", fileSchema, "NameOfCollection");

const growingUnit= mongoose.model('Growing Unit', growingUnitSchema);

'Growing Unit'에 일부러 공간이 있었는데 항상 빈 배열을 반환했습니다.이 공간을 제거하여 'Growing Unit'이 되는 것이 제 시나리오에서 필요한 해결책이었습니다.

const growingUnit= mongoose.model('Growing Unit', growingUnitSchema);

일반적인 "hello world" 문제(때로는 몽구스와 관련이 없는 문제도 있음).

  1. 컬렉션이 실제로 비어 있지 않은지 확인합니다(mongoDB 아틀라스 스크린샷).

enter image description here

  1. 작은 철자 차이 확인(예:listing대신에listings컬렉션에서 명령을 쿼리합니다.

  2. 올바른 사용 방법을 확인합니다.URI연결(예: 에 존재하는 컬렉션에서 데이터를 검색하려고 합니다.localhost그러나 사용mongoDB cluster(클라우드) - 또는 - 연결 문자열 URI와 관련된 기타 문제.https://docs.mongodb.com/manual/reference/connection-string/

나에게 문제는.skip(value),난 지나가고 있었다.page=1대신에page=0저는 레코드가 적었기 때문에 항상 빈 배열을 받았습니다.

언급URL : https://stackoverflow.com/questions/14183611/mongoose-always-returning-an-empty-array-nodejs

반응형