文档中的node.js猫鼬子字段

时间:2018-06-29 20:17:43

标签: node.js mongodb mongoose

一段时间以来,我一直在使用node.js和mongoose,但遇到了麻烦。我有一个包含20,000个文档的数据库,当我从cli搜索数据库时,它运行良好。

public class ActionSync extends AsyncTask<String, Void, Boolean> {
RestClient client;
IData activity;
private static String token;

public ActionSync(Context context) {
    activity = (IData) context;
}

@Override
protected Boolean doInBackground(String... params) {
    Retrofit.Builder builder = new Retrofit.Builder()
            .baseUrl("http://" + params[2] + ":" + params[3] + "/")
            .addConverterFactory(GsonConverterFactory.create());
    Retrofit retrofit = builder.build();
    client = retrofit.create(RestClient.class);

    //Authenticate the user.
    authenticateUser(params[0], params[1]);

    //Once the authentication has been completed, we can now move on the syncing data.
    syncUser(params[0]);




    //If we get to the end without any errors, then return true. This means that the sync is complete.
    return true;
}

@Override
protected void onPostExecute(Boolean loggedIn) {
    super.onPostExecute(loggedIn);
}

public void authenticateUser(String username, String password){
    //First, we must log into the server given our credentials. Otherwise, we will not be able to get any data.
    Login login = new Login(username, password);

    Call<Token> call = client.login(login);

    call.enqueue(new Callback<Token>() {
        @Override
        public void onResponse(Call<Token> call, Response<Token> response) {
            if(response.isSuccessful()){
                Log.v("ActionSync", "Login was successful");
                token = response.body().getKey();
            } else {
                Log.v("ActionSync", "Login was unsuccessful");
            }
        }

        @Override
        public void onFailure(Call<Token> call, Throwable t) {
            Log.v("ActionSync", "Error occurred during login");
        }
    });
}

public void syncUser(String username){
    Call<List<CustomUser>> call = client.getUser(token, username);

    call.enqueue(new Callback<List<CustomUser>>() {
        @Override
        public void onResponse(Call<List<CustomUser>> call, Response<List<CustomUser>> response) {
            Log.v("ActionSync", "The response from get user: " + response.toString());

        }

        @Override
        public void onFailure(Call<List<CustomUser>> call, Throwable t) {
            Log.v("ActionSync", "No response from get user");
        }
    });
}

public interface IData {
}

这将返回256个结果

模式

db.Tickets.find({ "Customers.Customer.CustomerID" : '123123123' })

尽管如果我使用猫鼬在node.js中对此进行测试。我无法返回任何东西

我的通用搜索有效

const mongoose = require('mongoose');
const Schema = mongoose.Schema;

// Define collection and schema for Ticket
var Ticket = new Schema({
    UserName: {
        type: String
    },
    Status: {
        type: String
    },
    TicketNumber: {
        type: Number
    },
    Name: {
        type: String
    },
    Description: {
        type: String
    },
    TicketTypeName: {
        type: String
    },
    DueDate: {
        type: Date
    },
    MapCollectDate : {
        type: Date
    },
    NumberofUsersAffected : {
        type: Number
    },
    DNNumber : {
        type : String
    },
    RevisionDate : {
        type : Date
    },
    CommercialImpact : {
        type: String
    },
    Customers :[{
        Customer: [{
            CustomerID: Number,
            CustomerName: String
        }]
    }],

但是无法使特定搜索生效。

我正在连接Mongo

Ticket.find(function (err, tickets){

日志输出

const config = require('./db');
//const Course = require('./models/Course');
//const CourseRoute = require('./routes/CourseRoute');
const Ticket = require('./models/Ticket');
const TicketRoute = require('./routes/TicketRoute');

const PORT = 4000;

mongoose.connect(config.DB).then(
    () => {console.log('Connected to MongoDB') },
    err => { console.log('Error connecting to MongoDB' +err)
    });

我的路线终点

Your node js server is running on PORT: 4000
Connected to MongoDB
Connected to MySQL

也尝试了不使用变量

router.route('/').get(function (req, res) {
        Ticket.find({ "Customers.Customer.CustomerID" : global.auth_username }, function(err, ticket) {            
        if(err){
            console.log(err);
        }
        else {
            res.json(tickets);
        }
    });
});

2 个答案:

答案 0 :(得分:0)

在运行查询之前忘记连接到Mongoose时,我遇到了同样的问题

mongoose.connect(MONGO_URL, mongoOptions)
    .then(() => {
        // do your thing here
    })

答案 1 :(得分:0)

您花了一年多的时间来解决这个问题,我相信您已经做到了,但是无论哪种方式,似乎您的代码中都有一个错字。回调参数名为ticket-function(err, ticket) {,而您正在记录tickets-res.json(tickets);。在通用测试中,您正确地编写了tickets-Ticket.find(function (err, tickets){,这可能就是它起作用的原因。

这里的重点是-使用调试工具而不是日志记录,可以更轻松地发现此类问题。

而且,一旦发现问题,就应该回答自己的问题。但是鉴于这可能完全没用,您最好将其删除。干杯!